如何删除选定的 DataGridViewRow 并更新连接的数据库表? [英] How to delete a selected DataGridViewRow and update a connected database table?

查看:10
本文介绍了如何删除选定的 DataGridViewRow 并更新连接的数据库表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 窗体应用程序(用 C# 编写)上有一个 DataGridView 控件.

I have a DataGridView control on a Windows Forms application (written with C#).

我需要的是:当用户选择一个 DataGridViewRow,然后单击删除"按钮时,应该删除该行接下来,需要使用表适配器更新数据库.

What I need is: when a user selects a DataGridViewRow, and then clicks on a 'Delete' button, the row should be deleted and next, the database needs to be updated using table adapters.

这是我目前所拥有的:

private void btnDelete_Click(object sender, EventArgs e)
{
    if (this.dataGridView1.SelectedRows.Count > 0)
    {
        dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
    }                
}

此外,这只会删除一行.我希望用户可以选择多行.

Furthermore, this only deletes one row. I would like it where the user can select multiple rows.

推荐答案

此代码删除dataGridView1的选定项:

 private void btnDelete_Click(object sender, EventArgs e)
 {
     foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
     {
         dataGridView1.Rows.RemoveAt(item.Index);
     }
 }

这篇关于如何删除选定的 DataGridViewRow 并更新连接的数据库表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆