3
votes

I've got a DataGridView with a BindingSource binding it to a DataTable. I've put a Delete button on my form and I want it to delete the currently selected row when clicked, something like this:

    if (dgvResourceSettings.CurrentRow != null && !dgvResourceSettings.CurrentRow.IsNewRow)
    {
        bindingSource.RemoveCurrent();
    }

The problem is that the New Row is visible on my datagridview. If the user selects that row and then clicks the Delete button then it deletes the last data row, ie the one above the New Row. The reason it does this is that when the datagridview loses focus it changes the current row to the last data row (if the current row was the New Row). I can verify this by simply tabbing off the datagridview.

So I wonder what the normal way to deal with this is? I know users could just select the row and press the DEL key, but I want to give them a Delete button also.

thanks.

UPDATE: From the answers/comments it seems that this behaviour is normal, so it's hard to have a Delete button and the New Row. I've opted to remove the New Row and have an Add and a Delete button instead.

4
hei i am facing same problem..can u plz tell me how u did this one?minakshi
As noted, I chose to remove the New Row (there's a property saying whether it's visible or not) and only have an Add and a Delete button instead. This makes the problem go away.Rory

4 Answers

3
votes

I have a delete button column in my DataGrid with a button for each row that has it's Tag set to the appropriate item. The click handler then retrieves the tag value and deletes that item. This avoids any issues with selection events, etc.

1
votes

The source must be this: "when the datagridview loses focus it changes the current row to the last data row". This is not the normal case, you must be doing something (keybd events?) to make that happen.

I have made delete buttons under a Grid and they worked fine.

Edit: After clarification, it is about the special status of the 'New Row', it is kind of virtual. I sort of see the reasoning behind that, suppose the selection didn't shift back when leaving the Grid. The CurrentRow would be null. In your case that would be OK, but often it wouldn't.

One idea: track the Current property (BindingSource) and disable the Delete button if no Row is current. Check how it works when a new row is (half) filled.

And otherwise, Jeff's idea looks good too. Or see BFree's answer on this question.

1
votes

The Solution is to use the "SelectedRows"-Property:

if (this.dataGridView1.SelectedRows.Count > 0)
  {
    foreach (DataGridViewRow dgvrCurrent in dataGridView1.SelectedRows)
  {
    if (dgvrCurrent == dataGridView1.CurrentRow)
      {
        dataGridView1.CurrentCell = null;
      }

    // Delete Row Here
  }
0
votes

One of the things I do with my DataGridView is include the ID column from the database in a hidden column. I also have a Button column in the datagridview, and if you click the button, it grabs the RowID of that row and sends it along to the database class.