Faster way to make DataGridViewRow invisible

I am using the following code to set a bunch of elements DataGridViewRow

to be invisible. The rule I'm using is to check the linked datasource for the boolean flag. If the flag is true, the string will be displayed. If not, he will be invisible.

The following code works, however it does it consuming quite a lot of time:

CurrencyManager currencyManager = (CurrencyManager)BindingContext[dataGridView.DataSource];


foreach (DataGridViewRow row in dataGridView.Rows)
    if (!objectList.list[row.Index].Selected)
        row.Visible = false;


Does anyone have a better solution? The longer the list of objects that I have to go through, the longer this process takes, naturally. I cannot set the range of cells because the booleans may not be contiguous.


source to share

1 answer

As PraVn said, you can just filter prior to using the datagridview. If you are using DataSet, DataTable or DataView, just do this:

DataSet ds = new DataSet();
ds.Tables[0].DefaultView.RowFilter = "YourBooleanColumn = 1";

DataView dv = new DataView();
dv.RowFilter = "YourBooleanColumn = 1";

DataTable dt = new DataTable();
dt.RowFilter.DefaultView.RowFilter = "YourBooleanColumn = 1";


Alternatively, you can filter at the end of the database (if there is one?). Let us know what your data source is and I will update as needed. This is the best I can do!



All Articles