在这段代码中,DataTable dt应该在删除行之前实例化,然后再将更改提交给数据库。

以下是修改后的代码:

private void button2_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); adapter.Fill(dt); dataGridView1.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex); MySqlCommandBuilder builder = new MySqlCommandBuilder(adapter); adapter.Update(dt); }

在这个修改后的代码中,我们首先实例化了一个DataTable对象,使用adapter.Fill()方法将数据填充到它里面。然后,我们删除了所选行,然后实例化了一个MySqlCommandBuilder对象来生成适配器的UPDATE命令。最后,我们使用适配器的Update()方法将更改提交到数据库。


原文地址: https://www.cveoy.top/t/topic/bb8q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录