gpt4 book ai didi

c# - 如何从 DataGridView 中删除选定的行?

转载 作者:太空狗 更新时间:2023-10-29 21:08:31 27 4
gpt4 key购买 nike

我有一个 DataGridView 和一个 Button。如果选择了行,我想通过单击按钮删除它们。我尝试了几个命令,如 RemoveAtSelectedRows 等,但没有任何效果。我该如何解决?

我试过类似的方法:

 if (dataGridView2.SelectedRows.Count > 0)
{

DataGridViewSelectedRowCollection rows = dataGridView2.SelectedRows;
dataGridView2.Rows.RemoveAt(rows);

}

但是 RemoveAt 方法只接受整数。在我尝试使用 Selected Cells 之前,他删除了所有行,因为总有一个单元格被选中。

最佳答案

如果您只想从 DataGridView 中删除选定的行,应该这样做:

foreach (DataGridViewRow row  in yourDataGridView.SelectedRows)
{
yourDataGridView.Rows.RemoveAt(row.Index);
}

您的代码不起作用,因为您使用了 RemoveAt(rows)RemoveAt 只接受您要删除的行的索引。您正在将 DataGridViewSelectedRowCollection 传递给它。您可以通过 DataGridViewRow.Index 获取一行的索引如上所示。

关于c# - 如何从 DataGridView 中删除选定的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28602700/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com