views:

7739

answers:

4

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.

+1  A: 

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.

Jeff Yates
+1  A: 

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.

Henk Holterman
Agreed, that's the source of the problem. It only happens when the currently selected row is the New Row, not if there is a normal data row selected. The only event of the dgv i'm handling is DataError (which isn't firing). I'm only setting a few dgv properties, none appear relevant. I can't see anything I'm doing that would cause this behaviour.
Rory
I see, but hten the question may be wrong. That 'new' row is a temp (until completed) and cannot be Deleted. Only Discarded.
Henk Holterman
This behavior is by design, it looks not easy to get the UI you want.
Henk Holterman
Weird - so a separate delete button is more or less incompatible with using the New row.
Rory
A: 

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.

Jared Harley
A: 

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
  }
timo2oo8