locked
delete all selected rows in DataGridView RRS feed

  • Question

  • I have code for delete selected rows:

    private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)

    {

    foreach (DataGridViewRow dr in dgwListSale.SelectedRows)

    {

    dgwListSale.Rows.Remove(dr);

    }

    }

    but this all right if selected not all rows in dgwListSalу, else i have exception:
    System.InvalidOperationException was unhandled
      Message="Uncommitted new row cannot be deleted."

    I changed code:

    private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)

    {

    foreach (DataGridViewRow dr in dgwListSale.SelectedRows)

    {

    if(dr.Cells[0].Value.ToString() != null) //Cells[0] - primary key

    dgwListSale.Rows.Remove(dr);

    }

    }

    and get new exception:
    System.NullReferenceException was unhandled
      Message="Object reference not set to an instance of an object."
    Help me
    Thursday, September 7, 2006 8:23 AM

Answers

  • If your dgv is in edit mode (not read only) are you sure you are not trying to delete the ever present "new" row found at the bottom of the grid and with an asterisk in the first column?  This row can be avoided by:

       If Not(dr.IsNewRow) Then dgwListSale.Rows.Remove(dr)

    Sorry, that is in VB and not C#.

    Thursday, September 7, 2006 12:00 PM

All replies

  • If your dgv is in edit mode (not read only) are you sure you are not trying to delete the ever present "new" row found at the bottom of the grid and with an asterisk in the first column?  This row can be avoided by:

       If Not(dr.IsNewRow) Then dgwListSale.Rows.Remove(dr)

    Sorry, that is in VB and not C#.

    Thursday, September 7, 2006 12:00 PM
  • Thank you

    Thursday, September 7, 2006 1:02 PM
  • i changed the code
    check first if all cells are selected

                if (DataGridView1.SelectedRows.Count == DataGridView1.Rows.Count)
                {
                    DataGridView1.Rows.Clear();
                }
                foreach (DataGridViewRow row in DataGridView1.SelectedRows)
                {
                    DataGridView1.Rows.Remove(row);
                }


    //DataGridView1 is my datagridview
    Sunday, December 31, 2006 8:16 AM
  • I am getting the same exception: Uncommitted new row cannot be deleted.  Unfortunately, it is not the same scenario so this fix did not apply to me.  Can you help?

     

    I am appending a new row to my grid when the user hits an Add button on my form.  When the user leaves that row, I want to prompt him at that time does he want to save this record to the database?  I am testing the No path.  He answers no and I want to delete the row from the grid.  I spent the day yesterday with the "Operation cannot be performed in this event handler" exception.  That happened in various event handlers.  Today I am getting "Uncommitted new row cannot be deleted" which at least seems much more informative, but I still don't know why it's not committed and can't be deleted.  Please also note, I have a Delete button on my form also and if the user types in the new row and clicks from the row to the Delete button the row is successfully deleted.  So what is happening in that path that I am missing?

     

    Private Sub dgvCustomers_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvCustomers.RowEnter

    If fCurrentOperation = CurrentOperation.Add Then

    MsgBox("row enter")

    MsgBox(Me.dgvCustomers.ReadOnly.ToString)

    Me.dgvCustomers.ReadOnly = True

    MsgBox(Me.dgvCustomers.ReadOnly.ToString)

    'MsgBox("deleting " & Me.dgvCustomers.Rows.Count - 1.ToString & "th row")

    'Me.dgvCustomers.Rows.Remove(Me.dgvCustomers.Rows(Me.dgvCustomers.Rows.Count - 1))

    MsgBox("deleting " & "548" & "th row")

    Me.dgvCustomers.Rows.Remove(Me.dgvCustomers.Rows(548))

    End If

    ' If MsgBox("Do you want to add this record to database?", MsgBoxStyle.YesNo, "RowEnter") = MsgBoxResult.No Then

    ' Me.dgvCustomers.Rows.Remove(Me.dgvCustomers.Rows(Me.dgvCustomers.Rows.Count - 1))

    ' End If

    ' 'ConfirmDatabaseChanges()

    ' 'fCurrentOperation = CurrentOperation.View

    'End If

    End Sub

     

    I added the ReadOnly=True based on this thread (because it was False) but that didn't help.  I was playing around with the index to see what row I could delete.  I guess none.  Here is the code behind my Delete button.

     

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

    Me.dgvCustomers.Rows.Remove(Me.dgvCustomers.CurrentRow)

    End Sub

     

    Does something get committed automatically when I leave the grid and hit the delete button and I'm therefore able to delete?

     

    Thank you.  This has been plaguing me for a while.

    Thursday, April 26, 2007 2:55 PM
  •  

    I experienced the same issue and after a little experimentation realized that I had the 'AllowUserToAddRows' property set to True.  This forces at least one row to always be present at the bottom of the grid (so that the user may click that row to initiate the 'Add' operation).

     

    Even if the 'AllowUserToAddRows' property is False, you may still programmatically add rows (i.e. the user may still click one of your controls to invoke code to add a row).  The user just won't be able to use that 'new row' row to do so, because when the 'AllowUserToAddRows' property is false, the row will not be there.

     

    So, set the 'AllowUserToAddRows' property to False and the 'Remove' and 'RemoveAt' calls will work as you expect... at the cost of more of your code to add rows.

    Monday, March 31, 2008 6:31 PM
  • Thank !
    Wednesday, October 27, 2010 1:35 AM
  • I have code for delete selected rows:

    private void bindingNavigatorDeleteItem_Click( object sender, EventArgs e)

    {

    foreach ( DataGridViewRow dr in dgwListSale.SelectedRows)

    {

    dgwListSale.Rows.Remove(dr);

    }

    }

    but this all right if selected not all rows in dgwListSalу, else i have exception:
    System.InvalidOperationException was unhandled
      Message="Uncommitted new row cannot be deleted."

    I changed code:

    private void bindingNavigatorDeleteItem_Click( object sender, EventArgs e)

    {

    foreach ( DataGridViewRow dr in dgwListSale.SelectedRows)

    {

    if (dr.Cells[0].Value.ToString() != null ) //Cells[0] - primary key

    dgwListSale.Rows.Remove(dr);

    }

    }

    and get new exception:
    System.NullReferenceException was unhandled
      Message="Object reference not set to an instance of an object."
    Help me

    Wednesday, October 27, 2010 1:36 AM
  • I use a Binding Navigator to control the display of the DataGridView for the DataGridView set the AllowUserToAddRows and AllowUserToDeleteRows properties to false this removes the *row at the end of the DataGridView.

    Have a + AddNewItem and a - DeleteItem buttons in the Binding Navigator the following code you would write in the

    bindingNavigatorDeleteItem_MouseDown or bindingNavigatorDeleteItem_Click. I use MouseDown because i usually want a user confirmation.

    If using MouseDown ensure that the user cannot click or double click whilst the MouseDown is taking place by temporarily disabling the

    Binding Navigaor

    this.bindingNavigator.Enabled = false;

     

     

    DataGridViewSelectedRowCollection selectedRows = this.dgvTestGrid.SelectedRows;

    foreach (DataGridViewRow dr in selectedRows)

    {

     

     

        this.dgvTest.Rows.Remove(dr);

    }

    this.bindingNavigator.Enabled = true;

    Sunday, November 7, 2010 1:26 AM