davidegironi / advanceddatagridview

A .NET WinForms DataGridView with advanced capabilities
392 stars 123 forks source link

Closing the filter popup menu when clicking the filter icon again #101

Closed ltob closed 1 year ago

ltob commented 1 year ago

When I click on the filter icon, a popup menu appears. Clicking the same filter icon again while the popup menu is open will leave the popup menu open (actually close the popup menu and reopen it). In the same case, MS Excel closes the popup menu... Would it be possible to set this behavior for ADGV as well?

Lubos T. ADGV1 ADGV2

davidegironi commented 1 year ago

Hello, sorry for my delay, I'm really busy this times. Anyway, that will not be so easy to do. Let me think about this... I'll write here as soon as I get something.

ltob commented 1 year ago

Thanks!

pgdelhiganesh commented 1 year ago

Hi, can we have both search & find text box in the toopstrip bar ?

image

davidegironi commented 1 year ago

Hello @pgdelhiganesh , that is another issue, anyway yes, you can. You have to attach a FilterStringChanged event, then change the filter accoring to the search textchange. Take a look at textBox_filterPatient on this form here https://github.com/davidegironi/dentned/blob/master/DentneD/Forms/FormPatients.cs as sample.

davidegironi commented 1 year ago

Hello @ltob I've almost succed in that, but "almost" is not enough to publish this mod. The problem is that if you click outside the component, on a Panel as example, I do not find any event to reset the visibility status container of the MenuStrip.(ColumnHeaderCellVisible). This way you have to click two times on the last selected MenuStrip. The visibility status container keeps the last visible MenuStrip. I don't like it that much so I write down here the changes that I've try. Unluckily I've no more time to spend on this. Also I don't want to make a code-mess just for this function. If you or someone else solves this issue please let me know. As for now I'll close this.

Changed methods OnMouseUp, MenuStrip_FilterChanged, MenuStrip_SortChanged of ColumnHeaderCell like below

        /// <summary>
        /// OnMouseUp event
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(DataGridViewCellMouseEventArgs e)
        {
            if (FilterAndSortEnabled && e.Button == MouseButtons.Left && _filterButtonPressed)
            {
                _filterButtonPressed = false;
                _filterButtonOver = false;
                RepaintCell();
                if (_filterButtonImageBounds.Contains(e.X, e.Y) && FilterPopup != null)
                {
                    //if this is not the last visible cell force shown
                    if (_columnHeaderCellVisible.ColumnHeaderCellId != _columnHeaderCellId)
                        _visibilityToggle = false;
                    if (!_visibilityToggle)
                    {
                        FilterPopup(this, new ColumnHeaderCellEventArgs(MenuStrip, OwningColumn));

                        //set current visibile
                        _columnHeaderCellVisible.ColumnHeaderCellId = _columnHeaderCellId;
                    }
                    //invert visibility toggle for next show
                    _visibilityToggle = !_visibilityToggle;
                }
            }
            else
            {
                _visibilityToggle = false;
                _columnHeaderCellVisible.ColumnHeaderCellId = _columnHeaderCellId;
            }
        }

        /// <summary>
        /// OnFilterChanged event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuStrip_FilterChanged(object sender, EventArgs e)
        {
            RefreshImage();
            RepaintCell();
            if (FilterAndSortEnabled && FilterChanged != null)
            {
                FilterChanged(this, new ColumnHeaderCellEventArgs(MenuStrip, OwningColumn));
                _visibilityToggle = false;
            }
        }

        /// <summary>
        /// OnSortChanged event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuStrip_SortChanged(object sender, EventArgs e)
        {
            RefreshImage();
            RepaintCell();
            if (FilterAndSortEnabled && SortChanged != null)
            {
                SortChanged(this, new ColumnHeaderCellEventArgs(MenuStrip, OwningColumn));
                _visibilityToggle = false;
            }
        }

and adding the follogin class and private variable to ColumnHeaderCell

        private string _columnHeaderCellId = Guid.NewGuid().ToString();
        private ColumnHeaderCellVisible _columnHeaderCellVisible = null;
        private bool _visibilityToggle = false;

        /// <summary>
        /// Visibiliy reference
        /// </summary>
        internal class ColumnHeaderCellVisible
        {
            private string _columnHeaderCellId;
            public string ColumnHeaderCellId
            {
                get
                {
                    return _columnHeaderCellId;
                }
                set
                {
                    _columnHeaderCellId = value;
                }
            }
        }

Then the constructor of ColumnHeaderCell needs to be changed like below:

        public ColumnHeaderCell(DataGridViewColumnHeaderCell oldCell, bool filterEnabled, ColumnHeaderCellVisible columnHeaderCellVisible)
            : base()
        {         
            _columnHeaderCellVisible = columnHeaderCellVisible;
           ...

And of course the any ColumnHeaderCell must be changed, on AdvancedDataGridView class the instance class that mantains the current visible cell is been instantiated and then used in the constructor private ColumnHeaderCell.ColumnHeaderCellVisible _columnHeaderCellVisible = new ColumnHeaderCell.ColumnHeaderCellVisible();