I want to keep the auto filter value of DataGridView in C# even after re-searching.

Asked 2 years ago, Updated 2 years ago, 138 views

DataGridView HeaderCell as follows:
Configure DataGridViewAutoFilterColumnHeaderCell to
I have implemented a simplified version of Excel auto filter.

foreach (DataGridViewColumn in DataGridView.Columns) {
     col.HeaderCell= new DataGridViewAutoFilterColumnHeaderCell (col.HeaderCell);
 }

Currently, the filter operation is fine, but
I want to keep the contents of the filter when I set it up when I searched again.
(Currently, the filter is not selected for each search.)

To do this, keep what you selected in the filter once before searching, and
After searching, the contents of DataGridView are updated and the filter value is
I think I need to filter it.

Retrieve filter selection value from outside DataGridViewAutoFilterColumnHeaderCell,
Is it possible to set the filter value and filter action?
Or is there no choice but to make the header part into a custom part?

Thank you for your cooperation.

c# winforms

2022-09-30 19:15

1 Answers

Checking the implementation of DataGridViewAutoFilterColumnHeaderCell, the reset operation is

private void ResetFilter()
{
    if(this.DataGridView==null)return;
    BindingSource source = this.DataGridView.DataSource as BindingSource;
    if(source==null||String.IsNullOrEmpty(source.Filter))
    {
        filtered = false;
        selectedFilterValue="(All)";
        currentColumnFilter=String.Empty;
    }
}

Filter in BindingSource does not appear to reset.


2022-09-30 19:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.