C# 如何在DataGrid中更新数据

C# 如何在DataGrid中更新数据,c#,windows-mobile,compact-framework,C#,Windows Mobile,Compact Framework,我正在尝试更新我的DataGrid,但不幸的是无法更新!我的应用程序有一个数据网格,数据从CSV文件加载到该网格。有些数据需要更新。但我无法找到正确的方式来反映网格上的更新 以下是我到目前为止的情况: // Creation of my DataGrid this.dataSource = new DataSet(); DataTable data = new DataTable("Products"); data.Columns.Add("Note", System.Type.Ge

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

以下是我到目前为止的情况:

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

但是网格没有更新。。我缺少什么?

我从来没有像
Update
AcceptChanges
这样的工具按照我想要的方式工作。我怀疑他们做的事情与我看来显而易见的不同

此外,我不知道是否链接了单独的数据表(即通过底层指针)。对DataTable进行更改可能会也可能不会转换为更改DataGridView中存储的数据

对我来说,我重新分配了它

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;
}
注意:我没有运行VS,所以我不确定代码是否100%准确,但它应该给你一个想法

作为评论,我想知道您添加专栏的版本与我使用的版本之间是否存在任何差异:

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

// Mine
data.Columns.Add("Note", typeof(string));

对于面临相同问题的开发人员,以下是我如何修复我的问题:

可以使用SetField方法更新网格行中的数据。假设您在变量
currentRow
中有要修改的行索引,下面是我修改数据的方式:

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");
如果需要从网格中删除行,可以使用
RemoveAt
方法:

dataGridTable.Rows.RemoveAt(rowIndexToDelete);  

谢谢你的指点。。我用稍微不同的方式解决了这个问题。。。我的时间连+1都没有。
dataGridTable.Rows.RemoveAt(rowIndexToDelete);