DataGridView UserDeletingRow Not Firing - c#

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.

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".

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.

Related

Is it possible to disable first row from clicking on it?

I got event CellMouseClick that is editing the selected Row, but I have filter also so , when I click on the first row new dialog is showing with the edit datagrid.Is it possible to disable only first row click in DataGridView,tried that but not working:
advancedDataGridView.Rows[0].ReadOnly=true;

With C# winforms, can I create a button that begins edit of a selected cell in datagridview?

I have a winforms app form with a datagridview
I know I can double click in a cell to edit it but I want a user of my program to be able to be able to have a button to begin edit of a/the selected cell in the datagridview (without having to move the mouse to it and double click it)
How can I do that?
if the user already selected a cell your button just need to active the edit with BeginEdit like so
MSDN link to method
dgvExample.BeginEdit(true);
the parameter of the method is used to select the complete cells content
or not.
you might check if there is already a cell selected, if not maybe use a default cell (e.g. 0,0) to edit

Selecting next row(a whole row, not cell) in datagridview after row deletion

I have a problem with deletion of rows from datagridview. I want the next row to be selected fully after the previous one was deleted, so I can delete multiple rows in a row by pressing "Delete" button. The problem is that after row deletion, I get the first cell of the next row selected, so when I press "Delete" I just erase the content of that cell and I have to click of the row header to be able to remove in with "Delete" button click.
Any ideas/help about how the about can be fixed will be highly appreciated.
Thanks a lot.
Regards,
Igor
UPDATE:SelectionMode is already set to FullRowSelect mode. I forgot to mention that I have textboxes embedded into the grid cells, and when I delete a row, the textbox in the first cell of the next row gets selected and therefore I can't delete this row by pressing Delete button: I need to select the whole row again by pressing the row header.
What if you change the SelectionMode to "FullRowSelect" in DataGridView properties?
Set the SelectionMode property of the DataGridView either from the Properties window or from the code like this,
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
This should solve your issue.

DataGridView CheckboxColumn header check box synchronize with rows?

I have made a checkbox inside the header of checkbox column. It works fine when i check it all rows in the DataGridView gets checked. What I want to do is to uncheked the header cell checkbox when a single row in the DataGridView is unchecked. I tried putting code in the CellValueChanged event that sets the header checkbox state. The problem is that CellValueChanged is fired when current cell loses focus. So if I click two or three times in the cell nothing happens, but when e select next cell the event is fired and the header cell checkbox state is invalidated.
In CurrentCellDirtyStateChanged event call the datagridview's CommitEdit(DataGridViewErrorContexts.Commit) method.
It commits the cell value and triggers the CellValueChanged event of corresponding cell.
tried CurrentCellDirtyStateChanged event?

Need to add DataGridViewCheckBoxColumn in a Winform Application

I need to put a Checkbox against each Row in a DataGridView. I am making use of DataGridViewCheckBoxColumn for the purpose. But here user is able to select more than 1 row. How to restrict the user from selecting multiple rows in a DataGridView?
You will have to do it yourself when the checkbox is checked.
If you want to respond immediately
when users click a check box cell, you
can handle the
DataGridView.CellContentClick event,
but this event occurs before the cell
value is updated. If you need the new
value at the time of the click, one
option is to calculate what the
expected value will be based on the
current value. Another approach is to
commit the change immediately, and
handle the
DataGridView.CellValueChanged event to
respond to it. To commit the change
when the cell is clicked, you must
handle the
DataGridView.CurrentCellDirtyStateChanged
event. In the handler, if the current
cell is a check box cell, call the
DataGridView.CommitEdit method and
pass in the Commit value.
Whichever method you use, when you handle the checkbox being checked, you will need to mark all the other checkboxes as unchecked. How you will do that depends on whether (and how) your DataGridView is data-bound.

Categories