C# 删除dataGridView中的选定行
我有一个dataGridView,我用文件列表填充它。我希望能够通过选择条目(单击条目),然后按delete键来删除其中一些条目。以下是我目前掌握的代码:C# 删除dataGridView中的选定行,c#,datagridview,C#,Datagridview,我有一个dataGridView,我用文件列表填充它。我希望能够通过选择条目(单击条目),然后按delete键来删除其中一些条目。以下是我目前掌握的代码: private void DataGrid_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Delete) { foreach (DataGridViewRow r in DataGrid.SelectedRows)
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);
}
}
}
}
问题在于,它将选定行定义为一次单击的所有行。我想删除所有突出显示的行。换句话说,如果一行未高亮显示,则不会选中该行 这应该行得通
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);
}
}
}
}
private void DataGrid\u KeyDown(对象发送方,KeyEventArgs e)
{
if(e.KeyCode==Keys.Delete)
{
Int32 selectedRowCount=DataGrid.Rows.GetRowCount(DataGridViewElementState.Selected);
如果(selectedRowCount>0)
{
for(int i=0;i
由于我不能直接发表评论(声誉点),John Saunders问了一个问题,为什么所确定的解决方案应该有效,而原来的帖子没有——因为它们非常相似。两者之间的区别在于,在原始版本中,For-Each语句用于迭代,这将强制对象引用到迭代对象,因此,如果删除其中一个,则引用的对象将出错(因为它是一个集合,即更改已引用的集合)。第二个命名解决方案用于(int i=0;i
您无法更改集合,因为您拥有的集合已被引用,因此它已被“锁定”…在第一个示例中,您使用DataGrid.SelectedRows作为迭代的元素,并在每次迭代后重新读取它
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();
}
该集合在每次迭代后下降一次…删除一次后,选定的行集合将减少
只需在迭代删除时避免修改集合。例如:
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) { }
List toBeDeleted=新列表();
尝试
{
foreach(DataGrid.SelectedRows中的DataGridViewRow行)
待删除。添加(行);
foreach(待删除的DataGridViewRow行)
DataGrid.Rows.Remove(行);
}
捕获(异常){}
我只是使用了一个反向循环,如下所示:
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);
}
}
这个例子是由一个按钮触发的,而不是按一个键触发的,但是变化很小。可以压缩代码,删除额外的变量行和长度。我尝试了这个方法。这很有效
string r1 = dataGridView1.CurrentRow.Cells[0].Value.ToString();
string r2 = dataGridView1.CurrentRow.Cells[2].Value.ToString();
string r3 = dataGridView1.CurrentRow.Cells[3].Value.ToString();
string r4 = dataGridView1.CurrentRow.Cells[4].Value.ToString();
string r5 = dataGridView1.CurrentRow.Cells[5].Value.ToString();
string r6 = dataGridView1.CurrentRow.Cells[6].Value.ToString();
string r7 = dataGridView1.CurrentRow.Cells[7].Value.ToString();
double prof = Convert.ToDouble(dataGridView1.CurrentRow.Cells[8].Value.ToString())
/ Convert.ToDouble(dataGridView1.CurrentRow.Cells[4].Value.ToString());
dataGridView2.Rows.Add(r1, rwcnt, r2, r3, r4, "", r5, r6, r7, prof);
//MessageBox.Show(prof.ToString());
dataGridView1.Rows.Remove(dataGridView1.CurrentRow); // remove current row
你试过检查行是否被选中了吗?我假设DataGrid中的所有行都被选中了。正如我所说的,有一种模式;它会获取在某个点上单击的所有行,只是单击其他位置不会取消选中它。选中的行和突出显示的行之间有什么区别?我没有得到这个部分e你说如果一行被点击一次,它总是被选中的!@nawfal:我不知道,我不会认为有什么不同,但是在DataGrid.SelectedRows中,有高亮显示的行和非高亮显示的行。请详细说明。为什么这行可以工作?为什么原始代码不工作?两者都可以工作,我不知道原始版本为什么不能工作这可能是因为“DataGrid.SelectedRows”在引用选择时包含选择的只读快照。请您解释一下为什么这段代码会回答这个问题?只回答代码,因为它们不会给出解决方案。(还有,注释掉的代码到底是干什么用的?)