【问题标题】:Removing rows from a WPF datagrid从 WPF 数据网格中删除行
【发布时间】:2012-12-11 11:50:59
【问题描述】:

我有一个 WPF DataGrid theDataGrid 绑定到包含一个表的 DataSet ds。我想让用户通过首先在网格中选择它们然后按下一个按钮(位于数据网格之外的某个位置)来删除线。我终于得到了以下代码行,它们可以满足我的需求,但我认为它们相当丑陋:

  DataSet ds = new DataSet();
        ...
  // fill ds somehow
        ...
  private void ButtonClickHandler(object Sender, RoutedEventArgs e) 
  {
        List<DataRow> theRows = new List<DataRow>();
        for (int i = 0; i < theDataGrid.SelectedItems.Count; ++i)
        {
            // o is only introduced to be able to inspect it during debugging
            Object o = theDataGrid.SelectedItems[i];
            if (o != CollectionView.NewItemPlaceholder)
            {
                DataRowView r = (DataRowView)o;
                theRows.Add(r.Row);
            }
        }
        foreach(DataRow r in theRows) 
        {                
            int k = ds.Tables["producer"].Rows.IndexOf(r);
            // don't remove() but delete() cause of update later on
            ds.Tables[0].Rows[k].Delete();
        }
   }

有没有更好的方法来做到这一点?例如。一种只需要一个循环而无需显式检查NewItemPlaceHolder 的方法,还是一种更有效的方法来访问要删除的行?

(我已经知道我不能在第一个循环中从 ds 中删除任何内容,因为每次执行循环时theDataGrid.SelectedItems.Count 都会更改...)

【问题讨论】:

  • 我有一个解决方案你想要我添加它吗??

标签: c# wpf data-binding datagrid


【解决方案1】:

为了删除按钮单击时选择的行,您可以尝试:

private void ButtonClickHandler(object sender, RoutedEventArgs e)//Remove row selected
     {
      DataRowView dataRow = (DataRowView)dataGridCodes.SelectedItem; //dataRow holds the selection
      dataRow.Delete();                    
     }

【讨论】:

    【解决方案2】:

    您可以通过向后迭代来删除双循环:

    private void ButtonClickHandler(object Sender, RoutedEventArgs e) {
        for (int i = theDataGrid.SelectedItems.Count-1; i>=0; --i)
            if (theDataGrid.SelectedItems[i] != CollectionView.NewItemPlaceholder)
                ds.Tables[0].Rows[i].Delete();
       }
    

    【讨论】:

      【解决方案3】:

      我认为它只需要一个循环即可:

      int count=theDataGrid.SelectedItems.Count;
      int removedCount=0; 
      while (removedCount < count)
      {
          try{
               Object o = theDataGrid.SelectedItems[0];
          }
          catch{ break;}
      
          if (o == CollectionView.NewItemPlaceholder)
          continue;
      
          DataRowView r = (DataRowView)o;
          r.Row.Delete();
          removedCount++;
      }
      

      【讨论】:

      • 嗯,谢谢,但没有。实际上,您在“编辑”之后添加的 foreach 循环是我的第一个“解决方案”。它会导致未处理的异常。这就是我在问题的最后一行中所指的,括号中的那个。 SelectedItems 集合在循环期间发生更改,并且 foreach 计数器引用了无效的内容(我认为)。我希望第一种方法也是如此。
      • @Thomas 你说的第二个是对的,但至少我确定Collection Modified Exception 不会在第一个中被抛出。
      • 它崩溃是因为您增加了最终变得大于(当前)SelectedItems.Count 的索引。如果您每次都检索 SelectedIndex[0],它可能会起作用。但是,老实说,手动更新的计数器并不比我的代码好。我想要的是像你的第二个建议一样的单线,但不会崩溃;-)
      • @Thomas 你是对的;对错误感到抱歉;正如你所说,我应该使用 SelectedIndex[0];我认为一个循环更有效,所以尝试使用它:)
      猜你喜欢
      • 2017-02-07
      • 1970-01-01
      • 2023-03-21
      • 2017-12-05
      • 2012-06-06
      • 2018-02-13
      • 1970-01-01
      • 2018-12-22
      相关资源
      最近更新 更多