【问题标题】:Faster Method to Making DataGridViewRow's non-Visible使 DataGridView 行不可见的更快方法
【发布时间】:2012-03-12 07:39:30
【问题描述】:

我正在使用以下代码将一堆DataGridViewRow 元素设置为不可见。我使用的规则是检查关联的数据源是否有布尔标志。如果标志为真,则将显示该行。如果没有,它将是不可见的。

以下代码有效;但是,这样做会消耗相当多的时间:

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();

有人有更好的解决方案吗?我必须经过的对象列表越长,这个过程自然需要的时间就越长。我无法设置单元格范围,因为布尔值可能不连续。

【问题讨论】:

  • 为什么不在绑定前过滤数据源?

标签: c# datagridview invisible datagridviewrow


【解决方案1】:

正如 PraVn 所说,您可以在使用 datagridview 之前进行简单的过滤。如果您使用的是 DataSet、DataTable 或 DataView,只需执行以下操作:

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";

或者,您可以在数据库端进行过滤(如果有的话?)。让我们知道您的数据源是什么,我会酌情更新。这是我能做的最好的了!

【讨论】:

  • 我很固执,尽量不使用多个列表。正确的解决方案是完全按照您所说的 PraVn 执行:过滤到新集合中。谢谢你们!
猜你喜欢
  • 2013-05-15
  • 1970-01-01
  • 2010-11-15
  • 2019-10-06
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 2020-03-21
  • 2012-05-30
相关资源
最近更新 更多