.net 如何使用删除按钮从datagridview中删除行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/777208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to delete row from datagridview with a delete button?
提问by Rory
I've got a DataGridView with a BindingSource binding it to a DataTable. I've put a Delete button on my form and I want it to delete the currently selected row when clicked, something like this:
我有一个带有 BindingSource 的 DataGridView 将它绑定到一个 DataTable。我在表单上放置了一个删除按钮,我希望它在单击时删除当前选定的行,如下所示:
if (dgvResourceSettings.CurrentRow != null && !dgvResourceSettings.CurrentRow.IsNewRow)
{
bindingSource.RemoveCurrent();
}
The problem is that the New Row is visible on my datagridview. If the user selects that row and then clicks the Delete button then it deletes the last data row, ie the one above the New Row. The reason it does this is that when the datagridview loses focus it changes the current row to the last data row (if the current row was the New Row). I can verify this by simply tabbing off the datagridview.
问题是新行在我的 datagridview 上可见。如果用户选择该行,然后单击删除按钮,则会删除最后一个数据行,即新行上方的数据行。这样做的原因是当 datagridview 失去焦点时,它将当前行更改为最后一个数据行(如果当前行是新行)。我可以通过简单地关闭 datagridview 来验证这一点。
So I wonder what the normal way to deal with this is? I know users could just select the row and press the DEL key, but I want to give them a Delete button also.
所以我想知道处理这个问题的正常方法是什么?我知道用户可以只选择行并按 DEL 键,但我也想给他们一个删除按钮。
thanks.
谢谢。
UPDATE: From the answers/comments it seems that this behaviour is normal, so it's hard to have a Delete button and the New Row. I've opted to remove the New Row and have an Add and a Delete button instead.
更新:从答案/评论看来,这种行为是正常的,所以很难有一个删除按钮和新行。我选择删除新行并改为使用添加和删除按钮。
采纳答案by Henk Holterman
The source must be this: "when the datagridview loses focus it changes the current row to the last data row". This is not the normal case, you must be doing something (keybd events?) to make that happen.
来源必须是这样的:“当 datagridview 失去焦点时,它会将当前行更改为最后一个数据行”。这不是正常情况,您必须做一些事情(keybd 事件?)才能实现。
I have made delete buttons under a Grid and they worked fine.
我在网格下制作了删除按钮,它们工作正常。
Edit:After clarification, it is about the special status of the 'New Row', it is kind of virtual. I sort of see the reasoning behind that, suppose the selection didn't shift back when leaving the Grid. The CurrentRow would be null. In your case that would be OK, but often it wouldn't.
编辑:澄清后,它是关于“新行”的特殊状态,它有点虚拟。我有点明白这背后的原因,假设选择在离开网格时没有移回。CurrentRow 将为空。在您的情况下,这可以,但通常不会。
One idea: track the Current property (BindingSource) and disable the Delete button if no Row is current. Check how it works when a new row is (half) filled.
一个想法:跟踪 Current 属性 (BindingSource) 并在当前没有 Row 时禁用 Delete 按钮。检查新行(一半)填充时它是如何工作的。
And otherwise, Jeff's idea looks good too. Or see BFree's answer on this question.
否则,杰夫的想法看起来也不错。或查看 BFree对此问题的回答。
回答by Jeff Yates
I have a delete button column in my DataGridwith a button for each row that has it's Tagset to the appropriate item. The click handler then retrieves the tag value and deletes that item. This avoids any issues with selection events, etc.
我有一个删除按钮列,DataGrid每行都有一个按钮,将其Tag设置为适当的项目。然后单击处理程序检索标记值并删除该项目。这避免了选择事件等的任何问题。
回答by timo2oo8
The Solution is to use the "SelectedRows"-Property:
解决方案是使用“SelectedRows”-属性:
if (this.dataGridView1.SelectedRows.Count > 0)
{
foreach (DataGridViewRow dgvrCurrent in dataGridView1.SelectedRows)
{
if (dgvrCurrent == dataGridView1.CurrentRow)
{
dataGridView1.CurrentCell = null;
}
// Delete Row Here
}
回答by Jared Harley
One of the things I do with my DataGridView is include the ID column from the database in a hidden column. I also have a Button column in the datagridview, and if you click the button, it grabs the RowID of that row and sends it along to the database class.
我使用 DataGridView 做的一件事是将数据库中的 ID 列包含在隐藏列中。我在 datagridview 中还有一个 Button 列,如果您单击该按钮,它会获取该行的 RowID 并将其发送到数据库类。

