views:

36

answers:

2

Consider 2 DataGridViews both with SelectionMode = RowHeaderSelect. In grid1 I have 2 columns; a TextBox and a CheckBox. In grid2 I have 2 columns; a ComboBox and a TextBox.

When I click on the RowHeader, the focus goes to the first cell, not the RowHeaderCell. When I press the DEL button, the contents of the cell is deleted (TextBox content is cleared and ComboBox is set to 1st item).

This way the UserDeletingRow event never fires as the DataGridView FAQ says "Users can delete rows only when the current cell is not in edit mode, the AllowUserToDeleteRows property is set to true".

As a workaround I implemented this code in RowHeaderMouseClick

grid.CurrentCell = grid.Rows[rowIndex].Cells[columnName];
grid.Rows[rowIndex].Selected = true;

In case of grid1 I pass the name of the CheckBox column and the UserDeletingRow event fires when I press DEL. However for grid2 passing the name of the ComboBox column doesn't fire the event.

I have tried the following:
1- Select RowHeaderCell -> Exception because it cannot be selected.
2- grid.EndEdit() -> No effect.
3- Change the SelectionMode to FullRowSelect then back to RowHeaderSelect -> Cell is set to edit mode.

My Question:
I need the UserDeletingRow to fire regardless of the column of the grid? Or a workaround to select the HeaderCell if possible?

Edit (Short form of the question):
Clicking the RowHeader selects the row and puts the first cell in edit mode, if applicable, this way clicking the DEL button deletes the cell contents and doesn't fire the UserDeletingRow event. As a workaround I select a cell, if available, that cannot be put in edit mode, e.g. CheckBoxColumn.

Is this the default behavior of clicking the RowHeader? If yes, can you show me a workaround? If no can you point me to the cause of this behavior?

Edit 2:
I have found that setting the EditMode to EditOnEnter makes clicking RowHeader put the first cell in edit mode. I will try to overcome this but any help is appreciated.

+1  A: 

It's a shame that I can't leave a comment yet (because of the rep stuff and so on!) Also you weren't clear on what exactly you want to do.

Clicking the RowHeader of the row results in the full row getting selected and pressing the Del button the row gets deleted and the event is fired!

If you want to be able to delete a row without selecting row header, consider using "Full Row Select" instead of "Row Header Select".

M2X
Clicking the RowHeader selects the entire row but put the first cell in edit mode. That's why pressing DEL deletes the cell content and doesn't fire the UserDeletingRow event.
sh_kamalh
Well I tried that right now, It didn't put my first cell in edit mode and I could easily delete that row. wonder if there's a property for that behavior you're mentioning...
M2X
@M2X when the EditMode is set to EditOnEnter clicking the RowHeader puts the first cell in edit. That's why DEL doens't fire the UserDeletingRow event.
sh_kamalh
Try this: On the "RowHeaderMouseClick" of the DataGridView call this.Validate() and try to delete the row by pressing DEL. "EditOnEnter" doesn't actually put the first cell on edit mode. It puts the last edited cell in edit mode IMO.
M2X
A: 

I have found the answer in http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/cab3c9eb-4c86-475e-8cbd-dee6b235765a. It is a workaround to change the EditMode to a mode other than EditOnEnter when the RowHeader is clicked then changing it back to EditOnEnter.

I want to thank @M2X because his comment was helpful.

sh_kamalh