Datagridview keypress handler extract current data.

  • Thread starter Thread starter Robert Styma
  • Start date Start date
R

Robert Styma

Hi,
I have a datagridview which is not linked to a database. I have added
keypress and keydown handlers for the table elements using the following code:

Private Sub DisplayContestantList_EditingControlShowing(ByVal sender As
Object, ByVal e As
System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles
DisplayReqsList.EditingControlShowing

If Me.HandlerAdded = False And Not e.Control Is Nothing Then
Dim tb As TextBox = CType(e.Control, TextBox)
'---add an event handler to the TextBox control---
RemoveHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf
DisplayReqsList_KeyPressEvent
AddHandler tb.KeyPress, AddressOf DisplayReqsList_KeyPressEvent
RemoveHandler DirectCast(e.Control, TextBox).KeyDown, AddressOf
DisplayReqsList_KeyDown
AddHandler tb.KeyDown, AddressOf DisplayReqsList_KeyDown
Me.HandlerAdded = True
End If
End Sub ' DisplaycontestantList_EditingControlShowing

Private Sub DisplayReqsList_KeyDown(ByVal eventSender As System.Object,
ByVal e As System.Windows.Forms.KeyEventArgs) Handles DisplayReqsList.KeyDown
Dim Row As Integer
Dim Col As Integer
Dim Val As String

Row = Me.DisplayReqsList.CurrentCell.RowIndex
Col = Me.DisplayReqsList.CurrentCell.ColumnIndex
Me.DisplayReqsList.UpdateCellValue(Col, Row)
Val = Me.DisplayReqsList.Item(Col, Row).Value.ToString

If e.KeyCode = Keys.F1 Then
MessageBox.Show(Val)
End If

End Sub ' DisplayContestantList_KeyPressEvent

Private Sub DisplayReqsList_KeyPressEvent(ByVal eventSender As
System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles
DisplayReqsList.KeyPress
Dim Row As Integer
Dim Col As Integer
Dim Val As String

Row = Me.DisplayReqsList.CurrentCell.RowIndex
Col = Me.DisplayReqsList.CurrentCell.ColumnIndex
Val = Me.DisplayReqsList.Item(Col, Row).Value.ToString

MessageBox.Show(Val)

End Sub ' DisplayContestantList_KeyPressEvent

The DataGridView is using the default "EditOnKeystrokeOrF2" setting.
The handlers execute as expected. However, in the handler, I attempt to get
the current data in the cell. It always shows up as blank or whatever it was
when the editing started. Charaters previously typed do not show up in "Val".

If I switch to "EditProgramatically", I can get the current data, but then I
have to
handle backspaces, highlighted areas, delete and such on my own.

What I am doing is allowing the user to type a few characters and then press
a function key. Based on what they typed in the cell, I take different
actions and
will update the cell. Kind of an auto-completion.

I am guessing the current data in the cell being edited is held in a
different control.
If so, is there a way to get to the control?
If there is please let me know.

My current workaround is to make the datagrid read only and pop a dialog box
with a textbox on double click of a cell. I then do the processing in the
text box
and put the data back when the dialog box closes. This is kind of clumsy.

Any suggestions are welcome.
 
Hello Robert ,

Welcome to Microsoft Managed Newsgroup, I'm Zhi-Xin Ye, it's my pleasure to
work with you on this issue.

The data typed in the editing control won't be committed until the editing
completes, so the value you retrieved from the current cell remains the
same while editing. To retrieve the data typed in the editing control, you
can get it from the TextBox.Text property directly. For example:

Private Sub DisplayReqsList_KeyPressEvent(ByVal eventSender As
System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles
DisplayReqsList.KeyPress

Dim tb As TextBox = CType(e.Control, TextBox)

MessageBox.Show(tb.Text)

End Sub ' DisplayContestantList_KeyPressEvent

Please try my suggestion and let me know whether it makes sense to you.

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Zhi-Xin Ye,
I am having a problem compiling the code you posted. I checked the
parameter names and types and they match what you specified. However. In the
KeyPress handler, the reference to e.control fails because
System.Windows.Forms.KeyPressEventArgs has no member named control. Trying
the same thing in the Keydown handler gives an error because the member
control in System.Windows.Forms.KeyEventArgs is a boolean indicating that the
control key is pressed. The compiler complains that boolean cannot be case
to textbox.

Please take another try. I do appreciaate the help.
--
Robert Styma
Principal Engineer (DMTS)
Alcatel-Lucent
 
Hi Robert,

I'm sorry for the careless, it should be as follows, casting the
eventSender instead.

Private Sub DisplayReqsList_KeyPressEvent(ByVal eventSender As
System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles
DisplayReqsList.KeyPress

Dim tb As TextBox = CType(eventSender , TextBox)

MessageBox.Show(tb.Text)

End Sub ' DisplayContestantList_KeyPressEvent

Sorry for the careless again. If you have any questions, please feel free
to let me know, I will be happy of assistance.

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ye,

The colonial times are over, please don't react in a way as if it is your
fault.
This is basic stuff, so as Robert does not know that you can as well reply
with.

"I see I did not put the casting in my reply".

Two times needless sorry in a message reminds me on colonial times, and that
is something I don't want to be remind too.

Cor
 
Hi Zhi-Xin Ye,
Your information put me on the right path.
The code which ended up working was:

Dim tb As DataGridViewTextBoxEditingControl
If eventSender.GetType Is GetType(DataGridViewTextBoxEditingControl) Then
tb = CType(eventSender, DataGridViewTextBoxEditingControl)
Else
Exit Sub
End If
MessageBox.Show(tb.Text, "KeyPress")


It worked with both the KeyPress and KeyDown handlers.
I have to check the type as the first keystroke came in as datagridview
on the eventSender and later ones came in as
DataGridViewTextBoxEditingControl.

Thank you very much.
 
Back
Top