Discussion:
DataGridView, how to capture a cell's KeyPress event
(too old to reply)
Komandur Kannan
2007-01-30 13:56:01 UTC
Permalink
Hi
I am working in vb.Net 2005 framework 2.0. How to capture the cell's
keypress event in datagridview. any great ideas.
Linda Liu [MSFT]
2007-01-31 03:53:45 UTC
Permalink
Hi Kannan,

DataGridView doesn't provided an event something like 'CellKeyPress' for us
to capture a cell's KeyPress event, because there're several kinds of
cells, e.g. DataGridViewTextBoxCell, DataGridViewComboBoxCell,
DataGridViewCheckBoxCell and so on, and not all DataGridView cells should
have the 'KeyPress' event.

Fortunately, DataGridView has provided another
event--EditingControlShowing, which brings us the inner editing control the
current cell is holding. We could get the editing control by handling the
EditingControlShowing event and then subscribe the KeyPress event of the
editing control.

The following is a sample.

public Form1()
{
InitializeComponent();
this.dataGridView1.EditingControlShowing += new
DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlSh
owing);
}

void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtbox = e.Control as TextBox;
if (txtbox != null)
{
txtbox.KeyPress += new
KeyPressEventHandler(txtbox_KeyPress);
}
}

void txtbox_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 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 or complex
project analysis and dump analysis issues. 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/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Komandur Kannan
2007-01-31 08:07:00 UTC
Permalink
Hi
Can you give the same below sample in vb.Net
Post by Linda Liu [MSFT]
Hi Kannan,
DataGridView doesn't provided an event something like 'CellKeyPress' for us
to capture a cell's KeyPress event, because there're several kinds of
cells, e.g. DataGridViewTextBoxCell, DataGridViewComboBoxCell,
DataGridViewCheckBoxCell and so on, and not all DataGridView cells should
have the 'KeyPress' event.
Fortunately, DataGridView has provided another
event--EditingControlShowing, which brings us the inner editing control the
current cell is holding. We could get the editing control by handling the
EditingControlShowing event and then subscribe the KeyPress event of the
editing control.
The following is a sample.
public Form1()
{
InitializeComponent();
this.dataGridView1.EditingControlShowing += new
DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlSh
owing);
}
void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtbox = e.Control as TextBox;
if (txtbox != null)
{
txtbox.KeyPress += new
KeyPressEventHandler(txtbox_KeyPress);
}
}
void txtbox_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}
Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 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 or complex
project analysis and dump analysis issues. 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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Linda Liu [MSFT]
2007-01-31 09:06:06 UTC
Permalink
Hi Kannan,

Here is a sample written in VB.NET.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler DataGridView1.EditingControlShowing, AddressOf
DataGridView1_EditingControlShowing
Me.DataGridView1.RowCount = 1
End Sub

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
Dim txtbox As TextBox = CType(e.Control, TextBox)
If Not (txtbox Is Nothing) Then
AddHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub


Private Sub txtBox_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
MsgBox(e.KeyChar.ToString())
End Sub

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
Komandur Kannan
2007-02-05 11:55:01 UTC
Permalink
Hi
Thank you very much. It worked fine but every tab this code
creates a new address space. That is If i click in one cell another cell the
value is called twice.
example
If I click n cell 1 and press A
And
If I click on cell 2 and press A
On the First time A occurs once. 2 time it occurs 2 twice and likewise

Parameswaran.N
Post by Linda Liu [MSFT]
Hi Kannan,
Here is a sample written in VB.NET.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler DataGridView1.EditingControlShowing, AddressOf
DataGridView1_EditingControlShowing
Me.DataGridView1.RowCount = 1
End Sub
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
Dim txtbox As TextBox = CType(e.Control, TextBox)
If Not (txtbox Is Nothing) Then
AddHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub
Private Sub txtBox_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
MsgBox(e.KeyChar.ToString())
End Sub
Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Linda Liu [MSFT]
2007-02-06 02:36:32 UTC
Permalink
Hi,

Thank you for your feedback.

I performed another test on my sample code and did see the problem you
described. The reason of the problem is that the KeyPress event of the
editing control is subscribed more than once. I didn't realize the editing
controls in different cells of same cell template are the same one.

To solve this problem, we should unsubscribe the KeyPress event of the
editing control when the current cell leaves focus.

The following is the modified sample code.

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler DataGridView1.EditingControlShowing, AddressOf
DataGridView1_EditingControlShowing
' subcribe the CellLeave event of the DataGridView
AddHandler DataGridView1.CellLeave, AddressOf
DataGridView1_CellLeave

Me.DataGridView1.RowCount = 1
End Sub

' add a field for the editing control to access it later
Dim txtbox As TextBox = Nothing

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
txtbox = CType(e.Control, TextBox)
If Not (txtbox Is Nothing) Then
AddHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub


Private Sub txtBox_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
MsgBox(e.KeyChar.ToString())
End Sub

Private Sub DataGridView1_CellLeave(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
' if the editing control is not nothing, unsubscribe the KeyPress
event
If Not (txtbox Is Nothing) Then
RemoveHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub
End Class

I have tested the above code on my machine and confirmed it works.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
Komandur Kannan
2007-02-06 04:40:01 UTC
Permalink
Hi
Thanks
Post by Linda Liu [MSFT]
Hi,
Thank you for your feedback.
I performed another test on my sample code and did see the problem you
described. The reason of the problem is that the KeyPress event of the
editing control is subscribed more than once. I didn't realize the editing
controls in different cells of same cell template are the same one.
To solve this problem, we should unsubscribe the KeyPress event of the
editing control when the current cell leaves focus.
The following is the modified sample code.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler DataGridView1.EditingControlShowing, AddressOf
DataGridView1_EditingControlShowing
' subcribe the CellLeave event of the DataGridView
AddHandler DataGridView1.CellLeave, AddressOf
DataGridView1_CellLeave
Me.DataGridView1.RowCount = 1
End Sub
' add a field for the editing control to access it later
Dim txtbox As TextBox = Nothing
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs)
txtbox = CType(e.Control, TextBox)
If Not (txtbox Is Nothing) Then
AddHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub
Private Sub txtBox_KeyPress(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
MsgBox(e.KeyChar.ToString())
End Sub
Private Sub DataGridView1_CellLeave(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
' if the editing control is not nothing, unsubscribe the KeyPress
event
If Not (txtbox Is Nothing) Then
RemoveHandler txtbox.KeyPress, AddressOf txtBox_KeyPress
End If
End Sub
End Class
I have tested the above code on my machine and confirmed it works.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
progress200710b
2008-09-30 08:40:55 UTC
Permalink
Hello

thanks for ur grest response.

Actully i m using a DataGridViewTextBoxCell

each time whrn i click on it the messagboxes increases.

i have handled the Cell Leave event
but it is not working

url:http://www.ureader.com/msg/15363865.aspx
Samuel Kamiru Mwangi
2010-04-28 14:02:21 UTC
Permalink
Hi Linda,
This is nice has sorted my problem partly.
How do I capture the Enter Key so that when I press the enter key, I move to
the next column instead to the next row.

Thanks.

Samuel.

url:http://www.ureader.com/msg/15363865.aspx

Loading...