How can I save all data from my DataGrid
when my button save is clicked?
This is my code and say error:
string value = (new System.Collections.Generic.Mscorlib_CollectionDebugView<SampleCrudApp.MainPage.GenerateNumber>(grdOrders.ItemsSource)).Items[0].items1;
Try this
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
}
}
This is just an example so you can see how to get the data:
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
You must keep in mind the types of each column to use the proper format in each case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.