Assaf Lavie
Assaf Lavie

Reputation: 75943

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?

Upvotes: 0

Views: 3850

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292405

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);
}

Upvotes: 1

Related Questions