[英]DataGridView with Data Binding - new Row not showing
I have a DataGridView
bound to List of a Class1
as in my previous question (By the way - the answer there was to use properties instead of fields) . 我有一个
DataGridView
绑定到一个列表Class1
为我刚才的问题 (顺便说一句- 回答有,而不是使用字段属性)。 I'm then adding a row with the following code: 然后,我使用以下代码添加一行:
l.Add(new Class1 { a = 5, b = 6 });
I checked and the Row is added to the List. 我查了一下行添加到列表中。 But the
DataGridView
is not being updated. 但是
DataGridView
尚未更新。 How is that fixed? 怎么固定的?
You have to re-Assign the Datasource, in case of any changes made in the bounded source, or that are in two way bounded : 如果在有界源中进行了任何更改,或者以两种方式有界 ,则必须重新分配数据源:
grid.DataSource = null;
grid.DataSource = l;
An answer to another question of mine solved this one as well. 我的另一个问题的答案也解决了这个问题 。 Use a
BindingSource
as an intermediate, and use: 使用
BindingSource
作为中间体,并使用:
bindingSource.Add(new Class1 { a = 5, b = 6 });
The property AllowUserToAddRows
of the DataGridView
must be true
. DataGridView
的属性AllowUserToAddRows
必须为true
。
The DataSource
for the DataGridView
must be a BindingSource
. DataGridView
的DataSource
必须是BindingSource
。 Its property AllowNew
must be true
. 它的属性
AllowNew
必须为true
。
You can create a BindingSource
(here named personBindingSource
) for the DGV in the Designer by editing its property DataSource
... The DataSource
of the BindingSource
might be a List<Person>
. 您可以创建一个
BindingSource
(这里命名personBindingSource
通过编辑其属性为DGV在设计器) DataSource
...的DataSource
中的BindingSource
可能是一个List<Person>
。
So this works fine... 所以这很好用...
// personBindingSource was already created in the Designer ...
personBindingSource.DataSource = null;
personBindingSource.DataSource = _lstPerson;
dgvPerson.DataSource = personBindingSource;
dgvPerson.Refresh();
The new line will be shown :-) 新行将显示:-)
This does not work... 这行不通...
dgvPerson.DataSource = _lstPerson;
dgvPerson.Refresh();
the DGV still contains all elements but in this case the new line will not be shown :-( DGV仍然包含所有元素,但是在这种情况下,将不显示新行:-(
Maybe this helps ... 也许这有帮助...
you should set it following way. 您应该按照以下方式进行设置。 after you add new item in the list, you must set Datasource null, and reassign it to dataGridview.
在列表中添加新项之后,必须将Datasource设置为null,然后将其重新分配给dataGridview。
List<Person> lst = new List<Person>();
private void button5_Click(object sender, EventArgs e)
{
lst.Add(new Person("X"));
lst.Add(new Person("y"));
dataGridView2.DataSource = lst;
lst.Add(new Person("Z"));
dataGridView2.DataSource = null;
dataGridView2.DataSource = lst;
}
public class Person
{
public Person(string fname)
{
this.firstname = fname;
}
public string firstname { get; set; }
}
you can use BindingSource, here I have example with Datatable, when any thing get changed in datasource it will reflected at the same time without any refresh function. 您可以使用BindingSource,这里有Datatable的示例,当数据源中发生任何更改时,它将在没有任何刷新功能的情况下同时反映出来。 If want to add new row, just need to update datatable.
如果要添加新行,只需更新数据表。
private BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = dt;
//datagridview column binding
ID.DataPropertyName = "ID";
Name.DataPropertyName = "Name";
grdCharges.DataSource = bindingSource;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.