我在 Winforms 应用程序中有一个 DataGridView,它有大约 1000 行(未绑定(bind))和 50 列。隐藏一列需要整整 2 秒。当我想隐藏大约一半的行时,这就成了一个问题。
private void ShowRows(string match)
{
this.SuspendLayout();
foreach (DataGridViewRow row in uxMainList.Rows)
{
if (match == row.Cells["thisColumn"].Value.ToString()))
{ row.Visible = false; }
else
{ row.Visible = true; }
}
this.ResumeLayout();
}
我通过在操作周围添加 Console.WriteLine(DateTime.Now)
进行了一些测试,row.Visible = false
绝对是慢一点。我是否遗漏了一些明显的东西,比如设置 IsReallySlow = false
?还是我必须继续启用虚拟模式并对必要的事件进行编码?
最佳答案
在我看来,您应该改为使用行过滤器。
尝试使用 DataView 作为绑定(bind)源并使用 DataView.RowFilter 来隐藏行或显示您选择的行。
DataGridView myGridView = new DataGridView();
DataView myDataView = myTable.DefaultView;
myGridView.DataSource = myDataView; // DataView that allows row filtering
myDataView.RowFilter = string.Format("thisColumn <> '{0}'",match); // this will hide all rows where "thisColumn" = match
关于c# - 在 DataGridView 中隐藏行非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2483235/