C# 在创建所有行后设置DataGridView行以停止刷新

C# 在创建所有行后设置DataGridView行以停止刷新,c#,.net,.net-3.5,datagridview,C#,.net,.net 3.5,Datagridview,我向数据网格视图中添加了大量行,这个过程非常缓慢,因为每次添加后都会尝试重新绘制 我很难在网上找到一个示例,说明如何创建一个行列表(或数组,无论哪种工作方式),并在创建列表后立即添加所有行。我需要这样做,以防止每次添加后重新绘制 有谁能给我举个简单的例子,或者给我推荐一个好的医生吗?以下是一些想法: 1) 将列表绑定到DataGridView。设置DataGridView.DataSource=MyList时,它会立即更新整个网格,而无需执行所有逐行操作。您可以将这些项添加到MyList,然后重

我向数据网格视图中添加了大量行,这个过程非常缓慢,因为每次添加后都会尝试重新绘制

我很难在网上找到一个示例,说明如何创建一个行列表(或数组,无论哪种工作方式),并在创建列表后立即添加所有行。我需要这样做,以防止每次添加后重新绘制


有谁能给我举个简单的例子,或者给我推荐一个好的医生吗?

以下是一些想法:

1) 将列表绑定到DataGridView。设置
DataGridView.DataSource=MyList
时,它会立即更新整个网格,而无需执行所有逐行操作。您可以将这些项添加到MyList,然后重新绑定到DataGridView。(我更喜欢使用BindingList,它将动态更新DataGridView网格。)


2) 如果数据绑定不是一个选项,我过去所做的就是在更新之前为每一列设置
AutoSizeMode=NotSet
,然后将其设置回之前的状态。真正减慢绘图速度的是
AutoSizeMode

您可能正在查找DataGridView.DataSource属性。看

例如:

//Set up the DataGridView either via code behind or the designer
DataGridView dgView = new DataGridView();

//You should turn off auto generation of columns unless you want all columns of
//your bound objects to generate columns in the grid
dgView.AutoGenerateColumns = false; 

//Either in the code behind or via the designer you will need to set up the data
//binding for the columns using the DataGridViewColumn.DataPropertyName property.
DataGridViewColumn column = new DataGridViewColumn();
column.DataPropertyName = "PropertyName"; //Where you are binding Foo.PropertyName
dgView.Columns.Add(column);

//You can bind a List<Foo> as well, but if you later add new Foos to the list 
//reference they won't be updated in the grid.  If you use a binding list, they 
//will be.
BindingList<Foo> listOfFoos = Repository.GetFoos();

dgView.DataSource = listOfFoos;
//通过代码隐藏或设计器设置DataGridView
DataGridView dgView=新建DataGridView();
//除非希望所有列都自动生成,否则应关闭列的自动生成
//绑定对象以在网格中生成列
dgView.AutoGenerateColumns=false;
//无论是在代码隐藏中还是通过设计器,您都需要设置数据
//使用DataGridViewColumn.DataPropertyName属性绑定列。
DataGridViewColumn=新DataGridViewColumn();
column.DataPropertyName=“PropertyName”//绑定Foo.PropertyName的位置
dgView.Columns.Add(column);
//您也可以绑定一个列表,但是如果以后向列表中添加新的foo
//引用它们不会在网格中更新。如果使用绑定列表,则
//会的。
BindingList ListoFoos=Repository.GetFoos();
dgView.DataSource=listofoos;

此时要绑定到的一个方便事件是DataGridView.DataBindingComplete,它在绑定数据源后触发。

是的,谢谢:)我知道我可以挂起控件图形。我试图通过只填充控件一次来绕过这一切,就像构建一个长字符串,然后将文本框设置为容纳它,而不是在构建时更新文本框100次。感谢您的详细描述:)