【问题标题】:removing selected rows in dataGridView删除 dataGridView 中的选定行
【发布时间】:2012-11-13 21:39:39
【问题描述】:

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

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

【问题讨论】:

  • 您是否尝试过检查该行是否被选中?
  • 我假设 DataGrid.Selected 行中的所有行都被选中。就像我说的,有一个模式;它会占用在某个时间点被点击的所有行,只是点击其他地方不会取消选择它。
  • 选中行和高亮行有什么区别?
  • 我没有得到你所说的如果一行被点击一次,它总是被选中的那部分!
  • @nawfal:我不知道,我不会认为有区别,但在 DataGrid.SelectedRows 中既有突出显示的行,也有未突出显示的行。

标签: c# datagridview


【解决方案1】:

这应该可行

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

【讨论】:

  • 请详细说明。 为什么应该这样工作? 为什么原来的代码不起作用?
  • 两者都应该可以工作,我不知道为什么原始版本不能工作,可能是因为“DataGrid.SelectedRows”在引用时包含选择的只读快照。
【解决方案2】:

在第一个示例中,您使用 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】:

    由于我无法直接发表评论(声誉积分),John Saunders 提出了一个问题,为什么确定的解决方案应该有效,而原始帖子却没有——因为它们非常相似。
    两者之间的区别在于,在原始的 For Each 语句中,它被用于迭代,它强制对象引用到被迭代的对象,因此如果一个被删除,引用的对象将被搞砸(因为它是一个集合,即更改集合你已经参考了)。第二个命名解决方案使用 for (int i = 0; i 您不能在 For Eaching 时更改您所拥有的集合 - 已经对其进行了引用,因此它被“锁定”..

    【讨论】:

      【解决方案4】:
      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(); }

      【讨论】:

      • 能否请您 edit 解释为什么这段代码回答了这个问题?纯代码答案是discouraged,因为它们不教授解决方案。 (另外,注释掉的代码到底是干什么用的?)
      【解决方案5】:

      我只是使用这样的反向 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);
              }
          }
      

      该示例是由按钮触发的,而不是通过按键触发的,但更改是微不足道的。可以精简代码,去掉多余的变量行和长度。

      【讨论】:

        【解决方案6】:

        我试试这个。这行得通。

        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
        

        【讨论】:

          猜你喜欢
          • 2015-05-04
          • 2016-08-02
          • 1970-01-01
          • 1970-01-01
          • 2011-06-05
          • 1970-01-01
          • 2013-08-01
          • 2018-08-08
          相关资源
          最近更新 更多