First move your data from the Data Grid view to datatable as:
DataTable dt = new DataTable(); DataColumn[] dcs = new DataColumn[]{}; foreach (DataGridViewColumn c in dgv.Columns) { DataColumn dc = new DataColumn(); dc.ColumnName = c.Name; dc.DataType = c.ValueType; dt.Columns.Add(dc); } foreach (DataGridViewRow r in dgv.Rows) { DataRow drow = dt.NewRow(); foreach (DataGridViewCell cell in r.Cells) { drow[cell.OwningColumn.Name] = cell.Value; } dt.Rows.Add(drow); }
Then move the data from the datatable table to sql using the foreach loop on datable.rows as:
foreach(DataRow dr in dt.Rows) { Product product = new Product();
The product is a separate class like:
public class Product { public int productId { get; set; } public string productName { get; set; } }
user1711092
source share