工作中经常需要用到DataGridView显示数据,需要在DataGridView中用到CheckBox,我的添加ChexkBox方法比较简单,代码如下:
DataTable table = new DataTable();
table.Columns.Add("choice", typeof(bool));
table.Columns.Add("id");
table.Columns.Add("name");
dataGridView.DataSource = table
table定义为全局变量,然后实现CheckBox全选
if (dataGridView.DataSource != null && table != null)
{
foreach (DataRow row in table.Rows)
{
row["chocie"] = true;
}
}
以上代码在DataGridView行数比较少的时候没有什么问题,当DataGridView行数超过200行时,界面就有点卡顿了,数据量再大点界面卡死更严重。不由联想到双缓冲绘图时,对画面的操作总是先在内存中进行,然后一次性贴图,这样大大提高了贴图速度。类似地,如果先在内存中处理好数据,再绑定到DataGridView中,理论上应该也能提高CheckBox勾选速度。经测试,添加2行代码后,CheckBox勾选速度得到很大提高!
if (dataGridView.DataSource != null && table != null)
{
dataGridView.DataSource = null;//
foreach (DataRow row in table.Rows)
{
row["chocie"] = true;
}
dataGridView.DataSource = table;
}