简体   繁体   中英

Save ONE row of DataGridView to database

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.

datadapter.update(datatable_name);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM