c# - 如何在更新列时暂停 DataGridView

标签 c# .net winforms datagridview

如何在更新列时暂停 .NET DataGridView 显示任何内容?

这是我当前的代码。它工作正常,但在 foreach 循环上非常慢;您可以看到水平滚动条随着每一列的添加而缓慢增长。我正在自己构建 UI 列,因为出于各种原因我不想使用 dataGridView1.AutoGenerateColumns。

// Disconnect and reset DataGridView
dataGridView1.DataSource = null;
dataGridView1.SuspendLayout();
dataGridView1.Columns.Clear();

// Get data from SQL
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter("select * from employeehist", conn);
adapter.Fill(dt);

// Build DataGridView columns
foreach (DataColumn c in dt.Columns)
{
    DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
    col.SortMode = DataGridViewColumnSortMode.NotSortable;
    col.DataPropertyName = c.ColumnName;
    col.HeaderText = c.Caption;
    dataGridView1.Columns.Add(col);
}

// Reconnect DataGridView
dataGridView1.DataSource = dt;
dataGridView1.ResumeLayout(true);

最佳答案

您可以将 VirtualMode 与 DataGridView 结合使用,以便非常有效地更新网格。参见这篇文章:http://msdn.microsoft.com/en-us/library/ms171622.aspx

据我所知,它似乎先更新整个集合,然后再更新 UI 上的任何内容,而不是为添加的每个新行添加到 UI 等。

关于c# - 如何在更新列时暂停 DataGridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4649995/

相关文章:

c# - 当实体匹配用户定义的查询/过滤器/规则时执行操作

c# - DateTime.Today 和 "static readonly"

C# 在工具箱中包含派生控件

c# - 如何按字母对字符串数组进行排序?

.net - 在Viewbox中获取TextBlock/Label进行缩放,而不会出现不必要的填充

具有模糊背景的 C# 对话框窗体

c# - MMC 进程立即关闭,无法链接到 Windows 窗体

c# - 将字符串从 jQuery 传递到 C# MVC

c# - 无法从 FILETIME(windows 时间)转换为 dateTime(我得到一个不同的日期)

c# - 异常后代码如何执行?