I am trying to update my DataGrid but unfortunately not able to! My application have a DataGrid to which data is loaded from a CSV file. Some of the data needs to get updated. But I am not able to find the right way to reflect the updates on the grid.
Here is what I have so far:
// Creation of my DataGrid
this.dataSource = new DataSet();
DataTable data = new DataTable("Products");
data.Columns.Add("Note", System.Type.GetType("System.String"));
data.Columns.Add("Details", System.Type.GetType("System.String"));
data.Columns.Add("Net", System.Type.GetType("System.String"));
data.Columns.Add("Empty Weight", System.Type.GetType("System.String"));
data.Columns.Add("Full Weight", System.Type.GetType("System.String"));
data.Columns.Add("Description", System.Type.GetType("System.String"));
data.Columns.Add("UOM", System.Type.GetType("System.String"));
data.Columns.Add("Item", System.Type.GetType("System.String"));
dataSource.Tables.Add(data);
dataGrid1.DataSource = data;
When User press a “Load” button, I load the data to my grid:
DataTable vehicle = dataSource.Tables[0];
.
. // data is read from CSV
.
vehicle.Rows.Add("A sample note", "...", full - empty, empty, full, "Test description", "Gr", i); // an example
Here is how I tried to update the data on the grid:
DataTable vehicle = dataSource.Tables[0];
vehicle.Rows[0].BeginEdit();
vehicle.Rows[0].ItemArray[0] = "TEST COMPLETE";
vehicle.Rows[0].EndEdit();
vehicle.AcceptChanges();
dataGrid1.Update();
But there is no update to the grid.. What am I missing?
For developers facing the same issue, here is how I fixed mine:
You can make use of the SetField method to update the data in the Grid’s row. Assuming that you have the row index to modify in variable
currentRow, there is how I modified my data:If you need to remove the row from the grid, you can make use of the
RemoveAtmethod: