0

I am working with a simple application with c# What I wan't to do is that when I retrieve data from SQL Database into Datagrid, i want some of rows to deleting by selecting them and then clicking a button.

The code i used to retrieve data is shown below:

     SqlConnection conn = new SqlConnection("Server=MEO-PC;Database= autoser; Integrated Security = true");

        conn.Open();

        SqlCommand cmd = new SqlCommand("SELECT * From evidenc", conn);
        DataTable dt = new DataTable("dtList");
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        dtg.ItemsSource = dt.DefaultView;
        SqlDataAdapter adapt = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adapt.Fill(ds);

        conn.Close();

Also the code i tried for deleting the specific row is:

     if (dtg.SelectedIndex >= 0)
        {
            dtg.Items.RemoveAt(dtg.SelectedIndex);
        }

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

I don't know where is the problem cause I'am new to programming. Thanks to everyone

1
  • You are loading the DG from the data table. The row number in the table and the row number in the DG are the same. So remove the item from the DataTable. Then refresh DG by using following : dtg.ItemSource = null; dtg.ItemSource = dt.DefaultView; Commented Jan 30, 2018 at 18:59

2 Answers 2

1

You need to remove the row from the DataTable. Try this:

DataRowView drv = dtg.SelectedItem as DataRowView;
if(drv != null)
{
    DataView dataView = dtg.ItemsSource as DataView;
    dataView.Table.Rows.Remove(drv.Row);
}

You can't remove an item from the Items collection when you have set the ItemsSource property.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much
1

You can use IEditableCollectionView to do this. You can directly change the underlying collection, if it allows changes to be made, by using the methods and properties that IEditableCollectionView exposes, regardless of the collection's type.

IEditableCollectionView iecv = CollectionViewSource.GetDefaultView(theDataGrid.ItemsSource) as IEditableCollectionView;

 while (theDataGrid.SelectedIndex >= 0)
            {
                int selectedIndex = theDataGrid.SelectedIndex;
                DataGridRow dgr = theDataGrid.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as DataGridRow;
                dgr.IsSelected = false;

                if (iecv.IsEditingItem)
                {
                    // Deleting during an edit!
                    iecv.CommitEdit();
                    iecv.RemoveAt(selectedIndex);
                }
                else
                {
                    iecv.RemoveAt(selectedIndex);
                }
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.