0

我正在尝试更新我的 DataGrid,但不幸的是无法更新!我的应用程序有一个从 CSV 文件加载数据的 DataGrid。一些数据需要更新。但我无法找到正确的方法来反映网格上的更新。

这是我到目前为止所拥有的:

// 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;

当用户按下“加载”按钮时,我将数据加载到我的网格中:

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

以下是我尝试更新网格上数据的方法:

DataTable vehicle = dataSource.Tables[0];
vehicle.Rows[0].BeginEdit();
vehicle.Rows[0].ItemArray[0] = "TEST COMPLETE";            
vehicle.Rows[0].EndEdit();
vehicle.AcceptChanges();
dataGrid1.Update();

但是网格没有更新..我错过了什么?

4

2 回答 2

1

我从来没有得到过像我想要的那样UpdateAcceptChanges按我想要的方式工作的工具。我怀疑他们做的事情与我认为显而易见的事情不同。

Also, I have no knowledge if a separate DataTable is linked (i.e. via an underlying pointer). Making a change to the DataTable may or may not translate to changing the data stored in the DataGridView.

For me, I reassign it.

private void UpdateTheDataGrid() {
  DataTable vehicle = (DataTable)dataGrid1.DataSource;
  // vehicle.Rows[0].BeginEdit(); <- Unsure if this is even needed
  vehicle.Rows[0].ItemArray[0] = "TEST COMPLETE";            
  // vehicle.Rows[0].EndEdit();
  vehicle.AcceptChanges();
  dataGrid1.DataSource = vehicle;
}

Another technique would be to work directly with the DataGridView control itself:

private void UpdateTheDataGrid() {
  dataGrid1.Rows[0].Cells[0].Value = "TEST COMPLETE";
}

NOTE to Above: I do not have VS running, so I'm not sure if that code is 100% accurate, but it should give you the idea.

As a comment, I am interested to know if there is any difference between your version of adding a column and the version I use:

// Yours
data.Columns.Add("Note", System.Type.GetType("System.String"));

// Mine
data.Columns.Add("Note", typeof(string));
于 2012-06-06T12:13:59.273 回答
1

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:

DataTable vehicle = dataSource.Tables[0];
vehicle.Rows[this.currentRow].SetField(4, "Updating the 5th column in the grid");
vehicle.Rows[this.currentRow].SetField(2, "Updating the 3rd column in the grid");

If you need to remove the row from the grid, you can make use of the RemoveAt method:

dataGridTable.Rows.RemoveAt(rowIndexToDelete);  
于 2012-06-11T07:54:22.277 回答