简体   繁体   中英

How to update only one row in a DataGridView?

I have a DataGridView that's populated from a DataTableAdatper, as in this tutorial .

I guess since it's an ODBC MySQL connection, it's not generating DBDirectMethods for me.

My question is how do I update a specific row in the DB after the user has edited it in the grid? Do I need to specify a DeleteCommand in my adapter? When would it be called?

You usually don't need to create the DeleteCommand, UpdateCommand and InsertCommand yourself : a CommandBuilder can do it for you :

private void UpdateCurrentRow()
{
    DataRowView drv = dataGridView1.CurrentRow.DataBoundItem as DataRowView;
    DataRow[] rowsToUpdate = new DataRow[] { drv.Row };

    OdbcDataAdapter adapter = new OdbcDataAdapter("SELECT * FROM FOO", connection);
    OdbcCommandBuilder builder = new OdbcCommandBuilder(adapter);
    adapter.Update(rowsToUpdate);
}

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