C# 删除 dataGridView 中的选定行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13369273/
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
removing selected rows in dataGridView
提问by Lukas Bystricky
I have a dataGridView that I populate with a list of files. I'd like to be able to remove some of those entries by selecting the entry (by clicking it) and then pressing the delete key. Here's the code I have so far:
我有一个用文件列表填充的 dataGridView。我希望能够通过选择条目(通过单击它)然后按删除键来删除其中一些条目。这是我到目前为止的代码:
private void DataGrid_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
foreach (DataGridViewRow r in DataGrid.SelectedRows)
{
if (!r.IsNewRow)
{
DataGrid.Rows.RemoveAt(r.Index);
}
}
}
}
The problem is that it defines selected rows as all rows that were at one time clicked on. I would like to delete all highlighted rows. In other words, if a row is not highlighted it's not selected.
问题是它将选定的行定义为一次单击的所有行。我想删除所有突出显示的行。换句话说,如果一行没有被高亮显示,它就不会被选中。
采纳答案by Benjamin
This should work
这应该工作
private void DataGrid_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
Int32 selectedRowCount = DataGrid.Rows.GetRowCount(DataGridViewElementStates.Selected);
if (selectedRowCount > 0)
{
for (int i = 0; i < selectedRowCount; i++)
{
DataGrid.Rows.RemoveAt(DataGrid.SelectedRows[0].Index);
}
}
}
}
回答by Ken
Since I can not comment directly (reputation points) , John Saunders asked a question why the solution identified should work and the original post did not - since they are very similar.
The difference between the two is that in the Original a For Each statement is being used to iterate which forces object references to the iterated objected and therefore if one is removed the referenced object would be messed up (as it is a collection ie changing the collection you have referenced). The second as named solution uses for (int i = 0; i < selected which is simply getting the individual location (if you will) letter in mailbox[i], therefore it is the only object that is being dealt with and not the collection as a whole.
You can not change the collection while For Eaching what you have - the reference is already made to it and therefore it is 'locked'..
由于我无法直接评论(声誉点),John Saunders 提出了一个问题,为什么确定的解决方案应该有效而原始帖子却没有——因为它们非常相似。
两者之间的区别在于,在原始的 For Each 语句中,使用 For Each 语句进行迭代,这会强制对象引用到迭代对象,因此如果删除一个对象,则引用的对象将被搞乱(因为它是一个集合,即更改集合你已经引用了)。第二个命名解决方案用于 (int i = 0; i < selected ,它只是获取邮箱 [i] 中的单个位置(如果你愿意) 信件,因此它是唯一正在处理的对象而不是集合作为一个整体。
您不能在 For Eaching 时更改集合 - 已经对它进行了引用,因此它被“锁定”..
回答by Seokjin Seo
private: System::Void DeleteRow_Click(System::Object^ sender, System::EventArgs^ e)
{
/*Int32 rowToDelete = this->dataGridView1->Rows->GetFirstRow(DataGridViewElementStates::Selected);
do {
try{
if (this->dataGridView1->Rows[rowToDelete]->IsNewRow){
this->dataGridView1->Rows[rowToDelete]->Selected = false;
rowToDelete = this->dataGridView1->Rows->GetFirstRow(DataGridViewElementStates::Selected);
}
this->dataGridView1->Rows->RemoveAt(rowToDelete);
}
catch (Exception^ e){
}
} while ((rowToDelete = this->dataGridView1->Rows->GetNextRow(rowToDelete, DataGridViewElementStates::Selected)) != -1);
*/
Int32 selectedRowCount = this->dataGridView1->Rows->GetRowCount(DataGridViewElementStates::Selected);
for (int i = 0; i dataGridView1->Rows[this->dataGridView1->SelectedRows[0]->Index]->IsNewRow){
this->dataGridView1->Rows[this->dataGridView1->SelectedRows[0]->Index]->Selected = false;
selectedRowCount = this->dataGridView1->Rows->GetRowCount(DataGridViewElementStates::Selected);
i = -1;
}
else {
this->dataGridView1->Rows->RemoveAt(this->dataGridView1->SelectedRows[0]->Index);
}
}
this->dataGridView1->ClearSelection();
}
回答by Gogu CelMare
In the first example you use the DataGrid.SelectedRows as the elements of the iteration, and you re-read it after every iteration....
在第一个示例中,您使用 DataGrid.SelectedRows 作为迭代的元素,并在每次迭代后重新读取它....
That collection goes down by one after every iteration... The selected rows collection WILL decrease after you delete one.
每次迭代后,该集合会减少一个......删除一个后,所选行集合将减少。
Just avoid modifying the collections while iterating for removal. Example:
在迭代删除时避免修改集合。例子:
List<DataGridViewRow> toBeDeleted = new List<DataGridViewRow>();
try
{
foreach (DataGridViewRow row in DataGrid.SelectedRows)
toBeDeleted.Add(row);
foreach (DataGridViewRow row in toBeDeleted)
DataGrid.Rows.Remove(row);
}
catch (Exception) { }
回答by Angelo Mascaro
I just use a reverse for loop like this:
我只是使用这样的反向循环:
private void ButtonRemoveRows_Click(object sender, EventArgs e)
{
DataGridViewRow row;
int length;
length = _dataGridView.SelectedRows.Count;
for(int i = length - 1; i >= 0; i--)
{
row = _dataGridView.SelectedRows[i];
_dataGridView.Rows.Remove(row);
}
}
the example is triggered by a button and not by pressing a key, but the change is trivial. Code can be condensed removing the extra variables row and length.
该示例由按钮触发,而不是通过按键触发,但更改是微不足道的。可以精简代码,删除额外的变量行和长度。

