添加一行设置数据源后的DataGridView [英] Add a Row After Setting DataSource to Datagridview
问题描述
我有很多的数据源到数据网格结合相关的问题。我有一个DataGridView到我从列表中设置数据源
I had lots of questions related to datasource binding of datagrid. I had a DatagridView to which I am setting DataSource from a list
List<Myclass> li = new List<MyClass>();
MyClass O = new MyClass();
O.Name = "Aden";
O.LastName = "B";
O.Id = 12;
li.Add(O);
O = new MyClass();
O.Name = "Li";
O.LastName = "S";
O.Id = 22;
li.Add(O);
Mydgv.DataSource = li;
其中MyClass的是
Where MyClass is
public Class MyClass
{
public string Name {get; set;}
public string LastName {get; set;}
public decimal Id {get; set;}
}
现在想要一个新的行添加到我的DataGridView的
Now Want to add a new Row to My DataGridView
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
Mydgv.Rows.Add(row);
但它是不可能的它引发错误,因为数据源的DataGrid是绑定与列表里。
,所以我的第一个问题是我怎么能做到这一点?
But it is not possible it raise error because Datasource of Datagrid is Binded with List li. So My first question is how could I do this?
我的第二个问题是,
。对于这样做的我做了一个解决方案,以改变我的列表里和数据的新行添加到它,然后将其设置为数据源数据网格,但我认为它不是一个可行的解决方案有没有更好的解决办法?
My second question is, For doing this I made a solution to alter my List li and Add New row of data to it and then set it to datasource of datagrid but I think its not a feasible solution is there any better solution?
即使试图通过CurrencyManager的
Even tried to do it by CurrencyManager
CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[MyGrid.DataSource];
currencyManager1.SuspendBinding();
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
Mydgv.Rows.Add(row);
currencyManager1.ResumeBinding();
据我所知,通过它,我暂停绑定的DataGrid,因为我在一个执行提供格式化网格我的帖子让行可见假。
但是为什么它不添加行的DataGrid这里工作?
As I know through it I suspended Binding of DataGrid to provide formatting to Grid as I performed in one of my post Make Row Visible false. But why it doesn't work here in adding row to datagrid?
推荐答案
尝试使用的BindingList,而不是List
Try using a BindingList instead of List:
BindingList<MyClass> li = new BindingList<MyClass>();
然后从列表本身添加或删除记录:
Then you add or delete records from the list itself:
li.Add(new MyClass() { Id = 15, LastName = "Z", Name = "Agent" });
和电网就会自动显示新行。
and the grid will automatically show that new row.
要拥有个人财产自动更新出现在网格中,那么你的类需要实现INotifyPropertyChanged接口:
To have the individual property updates automatically appear in the grid, then your class needs to implement the INotifyPropertyChanged interface:
public class MyClass : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
,然后你的财产将不得不引发事件:
and then your property would have to raise the event:
private string lastName = string.Empty;
public string LastName {
get { return lastName; }
set {
if (value != lastName) {
lastName = value;
OnPropertyChanged("LastName");
}
}
}
现在,如果你更新一行从代码中,网格将显示更新:
Now if you update a row from code, the grid will show that update:
li[1].LastName = "Q";
另见的实施INotifyPropertyChanged的 - 做一个更好的方式存在
这篇关于添加一行设置数据源后的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!