点击我的按钮保存后,如何保存DataGrid
的所有数据?
这是我的代码并说错误:
string value = (new System.Collections.Generic.Mscorlib_CollectionDebugView<SampleCrudApp.MainPage.GenerateNumber>(grdOrders.ItemsSource)).Items[0].items1;
答案 0 :(得分:0)
试试这个
private void btnSave_Click(object sender, EventArgs e)
{
//get data back from the datagrid view
//assuming my gridview is bound to ObservableCollection<User>
var users= dataGridView1.DataSource as ObservableCollection<User>;
foreach (var user in users)
{
//perform the save
}
}
答案 1 :(得分:0)
这只是一个示例,因此您可以看到如何获取数据:
For Each row As DataGridViewRow In dgv.Rows
For Each cell As DataGridViewCell In row.Cells
values.Add(cell.Value)
Next
sSQL = "INSERT INTO Table (value1,value2,value3) " & _
"VALUES (" & values(0) & "," & values(1) & "," & values(2) & ")"
'[...]
Next
在每种情况下,您必须牢记每列的类型以使用正确的格式。