从 WPF 数据网格中删除行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14031814/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:46:41  来源:igfitidea点击:

Removing rows from a WPF datagrid

c#wpfdata-bindingdatagrid

提问by Thomas

I have a WPF DataGrid theDataGridbound to a DataSet dscontaining a table. I want to enable the user to remove lines by first selecting them in the grid and then pressing a button (positioned somwhere outside of the datagrid). I finally arrived at the following lines of code which do what I want, but which I consider rather ugly:

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

Is there a better way to do this? E.g. one which needs only one loop and without having to check for the NewItemPlaceHolderexplicitly, or possible a more efficient way to access the rows which are to be deleted?

有一个更好的方法吗?例如,只需要一个循环而无需NewItemPlaceHolder显式检查的,或者可能是一种更有效的方式来访问要删除的行?

(I already figured out that I must not remove anything from the ds in the first loop, since then theDataGrid.SelectedItems.Countchanges everytime the loop is executed...)

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

回答by apomene

In order to remove row selected on button click you can try:

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

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

回答by Ramin

I think it works by just one loop:

我认为它只通过一个循环起作用:

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

回答by John B

You can remove the double loop by iterating backwards :

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

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