Faster Method to Making DataGridViewRow's non-Visible

瘦欲@ 提交于 2019-12-23 17:13:53

问题


I'm using the following code to set a bunch of DataGridViewRow elements to be invisible. The rule I am using is to check the associated datasource for a boolean flag. If the flag is true, the row will be displayed. If not, it will be invisible.

The following code works; however, it does so by consuming quite a bit of time:

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

currencyManager.SuspendBinding();

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

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


回答1:


As PraVn had said, you could simply filter prior to using the datagridview. If you are using a DataSet, DataTable, or DataView just do the 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 could filter at the database end (if there is one?). Let us know what your data source is and I'll update as appropriate. This is the best I can do!



来源:https://stackoverflow.com/questions/9663278/faster-method-to-making-datagridviewrows-non-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!