如何将gridview中的更改更新为我在代码中创建的数据表

时间:2012-06-14 14:32:34

标签: c# gridview datatable datasource gridcontrol

我在代码中创建datatable并将其设置为gridcontrolDevExpress component),以便像此处的代码一样显示(向datatable添加一行并设置它作为datasource)的gridcontrol

DataTable PersonFamiliesDataTable = new DataTable();

PersonFamiliesDataTable.Columns.Add("BirthDate", typeof(String));
PersonFamiliesDataTable.Columns.Add("Job", typeof(String));
PersonFamiliesDataTable.Columns.Add("NameFamily", typeof(String));

DataRow PersonFamilyRow = PersonFamiliesDataTable.NewRow();

PersonFamilyRow["BirthDate"] = DateTimePicker_FamilyInfo_BirthDate.Text;
PersonFamilyRow["Job"] = FamilyInfo_txt_job.Text;
PersonFamilyRow["NameFamily"] = FamilyInfo_txt_NameFamily.Text;
PersonFamiliesDataTable.Rows.Add(PersonFamilyRow);

grid_Family.DataSource = PersonFamiliesDataTable;

用户可以更改gridcontrol中的数据。我希望将gridcontrol的更改更新为datatable。我该怎么做(行编辑,行删除)?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:在Family_gridView_CellValueChanged事件中,添加以下代码:

PersonFamiliesDataTable = (DataTable)grid_Family.DataSource;

因此,在网格单元格发生任何更改后,我的datatable正在更新。请注意,当用户更改单元格并将焦点保留时,会出现此事件。