1

I am using c #, wpf, DataGrid, SqlDataAdapter, DataTable, MS Sql server On the main form there is a DataGrid and a button. Clicking on the button should be removed from the DataGrid selected lines. And then from the database. But this is not happening! Only from the DataGrid! Here is my code:

private void Button_DeleteSelectedRows_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (dataGrid.SelectedItems.Count == 1)
            {
                int selectedIndex = dataGrid.SelectedIndex;
                var row = dataTable.Rows[selectedIndex];
                row.Delete();

                dataAdapter.Update(dataTable);
            }
            else if (dataGrid.SelectedItems.Count > 1)
            {
                int count = dataGrid.SelectedItems.Count;

                for (int i = count - 1; i >= 0; i--)
                {
                    DataRowView rowView = dataGrid.SelectedItems[i] as DataRowView;

                    dataTable.AsEnumerable()
                               .Where(r => r["Name"].ToString() == rowView.Row["Name"].ToString())
                               .ToList()
                               .ForEach(r => r.Delete());
                    dataTable.AcceptChanges();
                }
                dataAdapter.Update(dataTable);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

}

P.S.: I do not use MVVM

Thank you very much for the help!

2
  • Is any exception caught? Commented Jun 1, 2016 at 3:53
  • Where does the data table come from? If it is generated then it is clear that this will not work however if it is a table from database direct this is another case. Commented Jun 1, 2016 at 5:08

4 Answers 4

1

This code works to delete selected rows. Be sure

while (Mydatagrid.SelectedItems.Count >= 1)
{
 DataRowView drv = (DataRowView)Mydatagrid.SelectedItem;
 drv.Row.Delete();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You may consider refactoring the for-loop into a while loop., e.g while(datagrid.SelectedItem.Count >=1). Generally you will encounter issues when accessing an collection based on index, in case if you delete an item within a loop.

Debug Tip: Try to check count of selected Items within the for-loop loop and check if the object exists @ particular index.

Comments

0

Everything is right I guess, to remove rows from DataTable use following syntax:

datatable.Rows.Remove((myGrid.SelectedItem as DataRowView).Row);

Or in second code block:

DataRowView rowView = dataGrid.SelectedItems[i] as DataRowView;
        datatable.Rows.Remove(rowView.Row);

then at last put

datatable.AcceptChanges();

changes should reflected properly.

Comments

0

I'm sorry for not having replied and kept you waiting! Your advice helped me a lot. I have decided this is the case:

else if (dataGrid.SelectedItems.Count > 1)
        {
            while (dataGrid.SelectedItems.Count > 0)
            {
             int selectedIndex = dataGrid.SelectedIndex;
            var row = dataTable.Rows[selectedIndex];
            row.Delete();

            dataAdapter.Update(dataTable);
            }
        }

[SOLVED]

currently I am thinking seriously alter the application to MVVM :D:D

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.