如何删除选定的 DataGridViewRow 并更新连接的数据库表?
我在 Windows 窗体应用程序上有一个 DataGridView
控件(用 C# 编写)。
我需要的是:当用户选择 DataGridViewRow,然后单击“删除”按钮时,应删除该行并且接下来,需要使用表适配器更新数据库。
这就是我到目前为止所拥有的:
private void btnDelete_Click(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
}
}
此外,这只删除一行。我希望用户可以选择多行。
I have a DataGridView
control on a Windows Forms application (written with C#).
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.
This is what I have so far:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
此代码删除
dataGridView1
的选定项目:This code removes selected items of
dataGridView1
:删除索引位于选定单元格中的行。因此,选择任何单元格,其相应的行将被删除。
Removes rows which indexes are in selected cells. So, select any cells, and their corresponding rows will be removed.
我写了以下代码,请看一下:
使用所选行的
Index
仍然可以工作;看看下面的代码是否可以解决问题:我希望这有帮助,问候。
I have written the following code, please take a look:
using the
Index
of the selected row still could work; see if the code below will do the trick:I hope this helps, regards.
该解决方案可以通过“e”参数删除一行(未选择,单击的行!)。
This solution can be delete a row (not selected, clicked row!) via "e" param.
也许您可以使用临时列表进行删除。用于忽略行索引更改
maybe you can use temp list for delete. for ignore row index change
要删除数据网格中的多行,
我的代码的 C# 部分:
To delete multiple rows in datagrid, c#
parts of my code:
好吧,这就是我通常从 DataGridView 中删除用户选中的行的方法,如果您将其与
Dataset
中的 DataTable 关联起来(例如:DataGridView1. DataSource = Dataset1.Tables["x"]
),那么一旦您在Dataset
中进行任何更新(删除、插入、更新),它将自动在您的中进行DataGridView
。Well, this is how I usually delete checked rows by the user from a
DataGridView
, if you are associating it with a DataTable from aDataset
(ex:DataGridView1.DataSource = Dataset1.Tables["x"]
), then once you will make any updates (delete, insert,update) in theDataset
, it will automatically happen in yourDataGridView
.当用户在 datagridview 中选择时,此命令删除所选行:
gvTest.Rows.RemoveAt(dvTest.CurrentRow.Index];
this command delete selected row when user selected in datagridview:
gvTest.Rows.RemoveAt(dvTest.CurrentRow.Index];
看看这样:
have a look this way:
试试这个:
Try this:
您首先从数据库中删除,然后然后更新您的
datagridview
:You delete first from the database and then you update your
datagridview
:例外不需要...或用您自己的代码进行更改。
代码:
DB:
示例:prntscr.com/p3208c
数据库集:http://prntscr.com/p321pw
Exception no need ... or change with your own code.
CODE:
DB:
Example: prntscr.com/p3208c
DB Set: http://prntscr.com/p321pw
试试这个:
Try this:
这对我有用:
This worked for me :
//这对我有用。数据源是一个BindingList
//This worked for me. The datasource is a BindingList
它对我有用!
It Work for me !
这是一个非常简单的示例:
ASPX:
代码隐藏:
您必须使用复选框或其他某种机制来允许用户选择多行,然后您可以浏览选中复选框的行,然后删除这些行。
here is one very simple example:
ASPX:
Code Behind:
you will have to use checkboxes or some other mechanism to allow users to select multiple rows and then you can browse the rows for ones with the checkbox checked and then remove those rows.