0

Guys I am trying to save ONE row of a DataGridView to a database.

Till now I have got examples where we need to SPECIFY the column names and numbers in z INSERT statemnt. as below.

string col1 = datagridview1[0, datagridview1s.CurrentCell.RowIndex].Value.ToString();
        string col2 = datagridview1[1, datagridview1.CurrentCell.RowIndex].Value.ToString();
        string col3 = datagridview1[2, datagridview1.CurrentCell.RowIndex].Value.ToString();

        string insert_sql = "INSERT INTO YourTable(col1,col2,col3)VALUES('"+ col1 +"','"+ col2 +"','"+ col3 +"'");
        this.getcom(insert_sql);
      }
      catch(Exception ex)
      { Message.Box(ex);}
     }

Is there any way to do the INSERT without having to hardcode the column details ? I want to save the row in the database using the column headers of the datagridview as the column name in database.

Anoushka Seechurn
  • 2,166
  • 7
  • 35
  • 52

1 Answers1

0

datadapter.update(datatable_name);

Sandy
  • 1
  • Please don't simply post the code. Give some explanation or information or usage about your code. For example, see [this answer](http://stackoverflow.com/a/16893057/756941). – Nazik Jan 16 '14 at 12:12