C#/Datagridview

Advertisement


Question
Datagridview
Datagridview
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.

Answer
On 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

Srini Nagarajan

Expertise

can answer any kind of questions in ASP.NET, C#, VB.NET, ASP, SharePoint 2007, Coldfusion, Powerbuilder 7.00 / 8.00, JAVA servlets, MS SQL 2000 / MSSQL7, Sybase

Experience

Contact me if you need any custom development on ASP.NET, ASP, Coldfusion, Powerbuilder

©2012 About.com, a part of The New York Times Company. All rights reserved.