C#/Datagridview
Expert: Srini Nagarajan - 7/23/2011
Question
I have add a datagridview control in a windows form from toolbox.I added columns by DataGridView tasks. I want to save the entire datagridview data into a table in the database when click a button. I have attach the form image for your reference.
AnswerOn click of the button you have to do the following:
This one pick first column and insert into a table called "Some_Table" Change to what every the table name and add the columns
private void Button_Click(object sender, EventArgs e)
{
for (int i = 0; i < DGV.Rows.Count; i++)
{
col1= Convert.ToString(DGV.Rows[i].Cells[1].Value);
Insert(col1);
}
}
private void Insert(String col1)
{
SqlCommand dbCommand = new col1();
dbCommand.CommandText = "INSERT INTO Some_TABLE(SomeCol1)VALUES (@col1param)";
dbCommand.Connection = conn;
da = new SqlDataAdapter();
da.SelectCommand = dbCommand;
dbCommand.Parameters.AddWithValue("@col1param",col1);
dbcommand.ExecuteNonQuery();
}
Please let me know if you have any questions