Question how to raise a datagridview.cellmouseclick event?

unclewalt

Member
Joined
Dec 8, 2009
Messages
5
Programming Experience
10+
this code correctly selects a desired datagridview cell:

DGV1.Rows(DGV1.RowCount - 1).Cells(0).Selected = True

but the problem is that the selected cell is not ready to accept input. (because the statment above occurs within the context of a datagridview.cellenter event?)
it seems it is first necessary to click on the cell to enable input.
therefore we have the thought to generate the appropriate cellclick via software to save the user the trouble.

all the documentation I can find addresses the issue of catching the cellclick and doing something special - which looks straightforward.
but we don't understand how to setup the appropiate parameters from the event generating side - nor perhaps the appropriate syntax of the event-generating statements. For example how do we specifically construct this parameter: System.Windows.Forms.DataGridViewCellMouseEventArgs?

i'm a little bit lost on this one. if anyone has a code fragment or other insight it would be greatly appreciated.
many thanks!
 
Thank you very much for the feedback but that reply does not appear to solve the problem the code is facing.

It still appears that it might be necessary to raise a DGV.CELLMOUSECLOCK event to resolve the problem.

I will try to do a better job of describing the problem. To summarize: the issue is that the code must apply some complex validation logic as data is entered into a datagridview. The user is tightly controlled as data is entered into an empty datagridview row cell by cell. After the user enters data into cells 0 – 3, validation logic is applied to the data when the 5th cellenter event occurs. If the data fails validation, the code generates an error in a message box, clears the data in cells 0 – 3, and selects cell 0 so that the user can attempt to start entering the data all over again. However even though cell 0 has been properly selected, it has all happened within the context of the DGV.cellenter event for cell(4). Evidently selecting cell(0) does not trigger VB.NET to leave cell(4). Only when the user subsequently clicks on cell(0) does the cell(4) leave event appear to get triggered.

This code adds the empty DGV row that the use must then manually populate:

Private Sub Button5_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
If addstylemode = False Then
DGV1.Rows.Add("", "", "", "", "", "",””)
init_a_row()
addstylemode = True
Else
cleanup_add_mode()
addstylemode = False
End If
End Sub

And we initialize the row like so – all the user can do is enter into cell(0) (except for a couple of emergency exit buttons)

Private Sub init_a_row()
DGV1.SelectionMode = DataGridViewSelectionMode.CellSelect
DGV1.Rows(DGV1.RowCount - 1).Cells(0).ReadOnly = False
DGV1.Rows(DGV1.RowCount - 1).Cells(0).Value = ""
For xx = 1 To 6
DGV1.Rows(DGV1.RowCount - 1).Cells(xx).ReadOnly = True
DGV1.Rows(DGV1.RowCount - 1).Cells(xx).Value = ""
Next
DGV1.Rows(DGV1.RowCount - 1).Cells(0).Selected = True
DGV1.Focus()
End Sub





As data is entered into each cell we control subsequent possibilities like so:

Private Sub DGV1_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV1.CellLeave
If addstylemode = True Then
If DGV1.CurrentCell.RowIndex = DGV1.RowCount - 1 Then
Dim mycol = DGV1.CurrentCell.ColumnIndex
Select Case mycol
Case 0
DGV1.Rows(DGV1.RowCount - 1).Cells(1).ReadOnly = False
DGV1.Rows(DGV1.RowCount - 1).Cells(1).Selected = True
Case 1
DGV1.Rows(DGV1.RowCount - 1).Cells(2).ReadOnly = False
DGV1.Rows(DGV1.RowCount - 1).Cells(2).Selected = True
Case 2
DGV1.Rows(DGV1.RowCount - 1).Cells(3).ReadOnly = False
DGV1.Rows(DGV1.RowCount - 1).Cells(3).Selected = True
End Select
End If
End If
End Sub

The crux of the problem arises when DGV.cell(3) is left and DGV.cell 4 is entered:

Private Sub DGV1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV1.CellEnter
If addstylemode = True Then
If DGV1.CurrentCell.RowIndex = DGV1.RowCount - 1 Then
If DGV1.CurrentCell.ColumnIndex = 4 Then
DGV1.CurrentCell.Style.BackColor = Color.White
CHECK_FOR_VALID_STYLEID()
End If
End If
End If
End Sub

Note in the code below that if validation fails an attempt is made to restore initial conditions:

Private Sub CHECK_FOR_VALID_STYLEID()
' MIGHT be a valid styleid - let's see ........
Dim teststyleid As String = DGV1.Rows(DGV1.RowCount - 1).Cells(0).Value
teststyleid &= ComboBox3.Text
For yy = 1 To 3
teststyleid &= DGV1.Rows(DGV1.RowCount - 1).Cells(yy).Value
Next
teststyleid = teststyleid.ToUpper
Dim MYSUB
MYSUB = Form5.SESSION.CreateUniSubroutine("READ.STYLE.REF", 2)
MYSUB.SETARG(0, teststyleid)
MYSUB.CALL()
Dim RESULT = MYSUB.GETARG(1)
If RESULT = "FAIL" Then
MsgBox("INVALID STYLEID. PLEASE TRY AGAIN.")
init_a_row()
Else
………
End If
End Sub

But again the problem seems to that although we’ve entered cell(4) the problem of leaving cell(4) does not appear to have been dealt with.

Changing editmode to editonenter seems to have no bearing on the solution of this problem. Evidently selecting the cell does not cause it to be entered.

I’ve written enough code to understand that doing complex data entry directly into a datagridview is a delicate task as cascading events and counter intuitive side effects can easily be triggered. Evidently that’s why people resort to throwing up textboxes disguised as datagridview cells – to gain better control of what’s going on.
If I can’t solve this problem or if I solve this one but find that other unexpected issues arise I will be headed in that direction myself!!
Many thanks for the help and in advance for any additional help you can provide on this particular issue.
uncle walt
 
Actually, I just read your first post again and the issue is that you are only selecting the cell, which means highlighting it. In order to edit a cell it must be the current cell. So, the solution is not to set the Selected property of the cell but rather to assign the cell to the grid's CurrentCell property. When you make the cell the current cell, it will immediately enter edit mode if you have set the grid's EditMode to EditOnEnter.
 
Back
Top