I had lost of question 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;
Where MyClass is
public Class MyClass
{
public string Name {get; set;}
public string LastName {get; set;}
public decimal Id {get; set;}
}
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);
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 another 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 feseable solution is there any better solution
i even try 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();
As i know throug it i Suspend Binding of DataGrid to Provide Formating to Grid As i Perform in One of my post Make Row Visible false
But why it doesn’t work here in adding row to datagrid
Please Provide me a detailed and exact solution of Problem
Thanks