Home Forums WPF controls Xceed DataGrid for WPF deleting items

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #23552 |

    Hi,

    I’m trying to get deleting working, and have run into a few problems.

    I copied this from another post in this forum;

    private void HandleDeleteRows(DataGridControl dataGridControl)
    {
    if (dataGridControl.SelectedItems.Count > 0)
    {

    ItemCollection itemCollection = dataGridControl.Items as ItemCollection;
    if (itemCollection != null)
    {
    foreach (MyObj item in dataGridControl.SelectedItems)
    {
    itemCollection.Remove(item);
    }
    dataGridControl.SelectedItems.Clear();
    dataGridControl.UpdateLayout();
    }
    }
    }

    However I get this exception;

    Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

    My item source;

    DataGridCollectionView temp = new DataGridCollectionView(MyCollection);
    grid.ItemsSource = temp;

    where MyCollection implements IBindingList

    What’s the best way to go about removing items from the underlying collection?

    Imported from legacy forums. Posted by Jack (had 4232 views)

    User (Old forums)
    Member
    Post count: 23064

    Possibly not the ideal way but I found that using a foreach loop of the selectedItems caused this error since the SelectedItems is modified as soon as you Remove the first element. Hence the foreach loop fails.

    I solved by using a for(int i=0; i<selectedItemCount; i++) style loop but remember that as you delete an item from the underlying itemCollection, the number of elements in the SelectedItems list also goes down.

    Imported from legacy forums. Posted by Derek (had 1124 views)

    User (Old forums)
    Member
    Post count: 23064

    What works for me, is to implement a Remove-function in the Collection:

    <b>Remove-function for the DataGrid:</b>
    private void DeleteSelectedRows(object sender, KeyEventArgs args)
    {
    DataGridControl grid = sender as DataGridControl;

    if (grid != null)
    {
    if (args.Key == Key.Delete)
    {
    if (grid.SelectedItems.Count > 0)
    {
    ArrayList selectedItems = new ArrayList(grid.SelectedItems);
    ListPersonnelInfo info = (grid.ItemsSource as DataGridCollectionView).SourceCollection as ListPersonnelInfo;

    if (info != null)
    {
    foreach (PersonnelInfo items in grid.SelectedItems)
    {
    info.Remove(items);
    }
    grid.SelectedItems.Clear();
    grid.UpdateLayout();
    }
    }
    }
    }
    }

    <b>Remove-function in ListPersonnelInfo:</b>

    public void Remove(PersonnelInfo perInfo)
    {
    if (this.List.Count > 0)
    {
    this.List.Remove(perInfo);
    }
    }

    Imported from legacy forums. Posted by Frances (had 3015 views)

    User (Old forums)
    Member
    Post count: 23064

    Has the ability to delete without writing any code been added since this thread was posted?

    Imported from legacy forums. Posted by Matthew (had 740 views)

    Xceed Support
    Member
    Post count: 5658

    No.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 1155 views)

    User (Old forums)
    Member
    Post count: 23064

    For a change I can share instead of ask. This method worked for me when wanting to delete selected rows from the datagrid. I normally have less than 10 rows in the datagrid so I am not sure if speed will be a problem with a large number of rows.

        private void Delete_Button_Click(object sender, RoutedEventArgs e)
        {
          if (dataGridControl1.SelectedItems.Count > 0)
          {
            DataGridCollectionView itemCollection = dataGridControl1.ItemsSource as DataGridCollectionView;

            object[] selectedItems = new object[ dataGridControl1.SelectedItems.Count];
            dataGridControl1.SelectedItems.CopyTo( selectedItems, 0);

            if (selectedItems != null)
            {
              foreach (System.Data.DataRow items in selectedItems)
              {
                itemCollection.Remove(items);
              }
              dataGridControl1.SelectedItems.Clear();
              dataGridControl1.UpdateLayout();
            }
          }
        }

     

    Imported from legacy forums. Posted by Kobus (had 3762 views)

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.