简体   繁体   English

如何只更新DataGridView中的一行?

[英]How to update only one row in a DataGridView?

I have a DataGridView that's populated from a DataTableAdatper, as in this tutorial . 本教程中所示 ,我有一个从DataTableAdatper填充的DataGridView。

I guess since it's an ODBC MySQL connection, it's not generating DBDirectMethods for me. 我猜因为这是ODBC MySQL连接,所以不会为我生成DBDirectMethods。

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? 我需要在适配器中指定DeleteCommand吗? 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 : 通常,您不需要自己创建DeleteCommand,UpdateCommand和InsertCommand:CommandBuilder可以为您做到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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