This is a migrated thread and some comments may be shown as answers.

Deleting Row from Grid or DataTable Associated with Grid

2 Answers 304 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jorge Gonzalez
Top achievements
Rank 1
Jorge Gonzalez asked on 09 Dec 2009, 09:46 PM
Hello,
    When trying to delete specific rows from the WPF grid I get this error when I execute this command:

grid_name.Items.RemoveAt(<position of row to delete>);

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

When I try deleting row 2 or greater from the datatable I get an error message saying that I've tried deleting an entry in the datatable that does not exist. Essentially an out of bounds error. Strangely enough I can delete the first row.

If I break the association between the grid and the datatable by doing this I can delete whatever row I want.

grid_name.ItemsSource = null;
datatable_name.Rows.RemoveAt(<position of row to delete>);
grid_name.ItemsSource = dataset_name.Tables[0].DefaultView; // I only have 1 datatable in the dataset.

Is this the correct way to delete stuff from the grid?

2 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 10 Dec 2009, 03:18 PM
Hi Jorge Gonzalez,

You should delete rows from the DataTable itself like this:

using System.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
using System.Data;
using System;
using System.Text;
 
namespace TicketID_265559_DeleteARowFromDataTable
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        DataTable dataTable;
 
        public Window1()
        {
            InitializeComponent();
 
            this.dataTable = new DataTable();
            dataTable.Columns.Add("StringColumn", typeof(string));
            dataTable.Columns.Add("DoubleColumn", typeof(double));
            dataTable.Columns.Add("DateTimeColumn", typeof(DateTime));
 
            Random random = new Random();
            for (int i = 0; i < 100; i++)
            {
                StringBuilder stringValue = new StringBuilder();
                for (int j = 0; j < 20; j++)
                {
                    stringValue.Append((char)random.Next(32, 126));
                }
 
                dataTable.Rows.Add(
                    stringValue.ToString()
                    , (random.NextDouble() * 100000) * (Math.Pow(-1, random.Next(0, 10)))
                    , DateTime.Now.AddDays(random.Next(-365, +365)));
            }
             
            this.grid.ItemsSource = dataTable.DefaultView;
        }
 
        private void OnDeleteSelectedRow(object sender, RoutedEventArgs e)
        {
            var index = this.grid.Items.IndexOf(this.grid.SelectedItem);
             
            if (index > -1)
            {
                this.dataTable.Rows.RemoveAt(index);
            }
        }
    }
}

I have attached a sample project that demonstrates this. I am not sure why your DataTable has only one row. You can try to modify my sample project to reproduce the behavior that you are experiencing and send it back. We will take a look.

Regards,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Jorge Gonzalez
Top achievements
Rank 1
answered on 24 Dec 2009, 10:34 PM

If the grid is not associated with the datatable default view use this:

grid_name.ItemsSource = null;
datatable_name.Rows.RemoveAt(<position of row to delete>);
grid_name.ItemsSource = dataset_name.Tables[0]; // No DefaultView appended
Tags
GridView
Asked by
Jorge Gonzalez
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Jorge Gonzalez
Top achievements
Rank 1
Share this question
or