简体   繁体   English

从WPF数据网格中删除行

[英]Removing rows from a WPF datagrid

I have a WPF DataGrid theDataGrid bound to a DataSet ds containing a table. 我有一个WPF DataGrid theDataGrid绑定到包含表的DataSet ds 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: 我终于得到了以下几行代码,它们做了我想要的,但我觉得它很难看:

  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? 有一个更好的方法吗? Eg one which needs only one loop and without having to check for the NewItemPlaceHolder explicitly, 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.Count changes everytime the loop is executed...) (我已经发现我不能在第一个循环中从ds中删除任何东西,因为每次循环执行时, theDataGrid.SelectedItems.Count改变...)

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM