删除dataGridView中的选定行

时间:2012-11-13 21:39:40

标签: c# datagridview

我有一个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);                        
                }
           }
     }
}

问题在于它将所选行定义为一次点击的所有行。我想删除所有突出显示的行。换句话说,如果未突出显示某行,则不会选中该行。

6 个答案:

答案 0 :(得分:5)

这应该有效

 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);  
        }
     }
   }
}

答案 1 :(得分:1)

由于我不能直接评论(声誉点),John Saunders问了一个问题,为什么确定的解决方案应该有效,原始帖子没有 - 因为它们非常相似。
两者之间的区别在于,原始a For Each语句用于迭代,强制对象引用迭代的对象,因此如果删除了一个引用的对象将被搞砸(因为它是一个集合,即更改集合你引用了)。第二个作为命名解决方案使用for(int i = 0; i&lt; selected,它只是获取邮箱[i]中的单个位置(如果你愿意的话),因此它是唯一被处理的对象,而不是整个集合。
你不能改变集合,而对于你拥有的东西 - 已经对它进行了引用,因此它被“锁定”了..

答案 2 :(得分:1)

在第一个示例中,您使用DataGrid.SelectedRows作为迭代的元素,并在每次迭代后重新读取它....

每次迭代后,该集合会减少一个...删除后,所选行集合将减少。

在迭代删除时避免修改集合。例如:

    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) { }

答案 3 :(得分:0)


    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();
}


答案 4 :(得分:0)

我只是使用反向for循环:

    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);
        }
    }

该示例由按钮而不是通过按键触发,但是更改是微不足道的。可以压缩代码来删除多余的变量行和长度。

答案 5 :(得分:0)

我尝试这个。这是可行的。

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
相关问题