Keypress handler for datagridview column edit

R

Robert Styma

I am trying add keypress handling to the cells of a DataGridView without
going to "Edit mode EditProgramatically" I want to force some cells to CAPS
only, others to numerics, and some to one of several letters. I am using
visual studio 2008


I am trying to understand the descrepency between a post on another forum
and my observations.
http://social.msdn.microsoft.com/Fo.../thread/a646b4fc-143a-4e87-9c64-8d5dc0260792/

This article has the following example:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 1 AndAlso TypeOf
e.Control Is TextBox Then
RemoveHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf
CellKeyPress
AddHandler DirectCast(e.Control, TextBox).KeyPress, AddressOf
CellKeyPress
End If
End Sub

Private Sub CellKeyPress(ByVal sender As Object, ByVal e As
KeyPressEventArgs)
Debug.Print(e.KeyChar)
End Sub


This implies that the keypress handler will handle the keypress for just
that cell.
Experimentation shows that once I set the handler, it fires when editing
every cell including new added rows.
Thus, using "EditMode EditOnKeystrokeOrF2", the following code seems to work.
Note that I add the handler once per display of the panel and then use the
keypress handler to pick out what column I am using.

Private HandlerAdded As Boolean = False
Private Sub DisplayClubList_EditingControlShowing(ByVal sender As
Object, ByVal e As
System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles
DisplayClubList.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
TextBox_KeyPress
AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
Me.HandlerAdded = True
End If
End Sub

Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
If Me.DisplayClubList.CurrentCell.ColumnIndex =
DisplayClubList.Columns("ClubCode").Index Then
e.KeyChar = e.KeyChar.ToString.ToUpper
End If
End Sub

My question is: Is the technique I am using in my code the way it is
supposed to work or am I coding to an anomoly?

Also, is this behavior consistant between .net 2.0 and .net 3.5?
 
Z

Zhi-Xin Ye [MSFT]

Hi Robert ,

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

The technique you use is better than the code in that thread. The KeyPress
handler is added only once, but works for all the DataGridViewTextBoxCells
in the grid.
Also, is this behavior consistant between .net 2.0 and .net 3.5?

As far as I know, there is no big difference for the DataGridView control
between .NET 2.0 and .NET 3.5, the code you run on the .NET 2.0 for
DataGridView can also run on the .NET 3.5.

If anything is unclear or you have any concerns, please do not hesitate to
let me konw, I will be happy of assistance.

Have a nice day!

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top