By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,617 Members | 1,605 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,617 IT Pros & Developers. It's quick & easy.

Detecting keypresses in a Datagrid cell

P: n/a
Does anyone know how to detect keypresses in a Datagrid cell? The
Keypress event ofthe datagrid doesn't fire if you are in a cell.
Neither does KeyUp & KeyDown. I've tried hooking in to the
DataGridCell events but it doesn't have the keypress event.

Robert

Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
robert_rowe wrote:
Does anyone know how to detect keypresses in a Datagrid cell? The
Keypress event ofthe datagrid doesn't fire if you are in a cell.
Neither does KeyUp & KeyDown. I've tried hooking in to the
DataGridCell events but it doesn't have the keypress event.


Guess:
set the KeyPreview property of the parent form to "True" and use the
KeyPress event of the form instead.

--
Konrad -
http://madrat.net/
Nov 20 '05 #2

P: n/a
* ro*********@yahoo-dot-com.no-spam.invalid (robert_rowe) scripsit:
Does anyone know how to detect keypresses in a Datagrid cell? The
Keypress event ofthe datagrid doesn't fire if you are in a cell.
Neither does KeyUp & KeyDown. I've tried hooking in to the
DataGridCell events but it doesn't have the keypress event.


You will have to take the 'KeyPress' event of the cell. In order to do
that, you will have to derive a class from 'DataGridColumnStyle' and
assign your own 'GridColumnStyle' of your 'TableStyle'. Then you add a
handler to the 'KeyPress' event of the class created in the last step.

Source code (taken from a post by Peter Fleischer):

\\\
....
Me.DataGrid1.TableStyles.Clear()
Dim dgts As New DataGridTableStyle
dgts.MappingName = "Tab1"
Me.DataGrid1.TableStyles.Add(dgts)
Dim myTextBoxColumn As DataGridTextBoxColumn = _
DataGrid1.TableStyles("Tab1").GridColumnStyles("Fe ld2")
Dim dgtb As DataGridTextBox = _
CType(myTextBoxColumn.TextBox, DataGridTextBox)
AddHandler dgtb.KeyPress, AddressOf meineTastenPresse
....

Private Sub meineTastenPresse(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs)
Debug.WriteLine("Gedrückt: " & Asc(e.KeyChar).ToString)
End Sub
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3

P: n/a
Hi Herfried,

Is there a reason why you use CType instead of DirectCast in this sample?

Cor
Nov 20 '05 #4

P: n/a
It worked great. Thanks.

Nov 20 '05 #5

P: n/a
* "Cor Ligthert" <no**********@planet.nl> scripsit:
Is there a reason why you use CType instead of DirectCast in this sample?


It's Peter Fleischer's example, not mine ;-). I didn't change anything
in the code.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #6

P: n/a
So why his and not mine?

I did not copied mine from his.

Cor
Nov 20 '05 #7

P: n/a
* "Cor Ligthert" <no**********@planet.nl> scripsit:
So why his and not mine?

I did not copied mine from his.


?!?

<URL:http://www.google.de/groups?selm=bmh3k0%24mhcuo%241%40ID-117757.news.uni-berlin.de>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #8

P: n/a
Hi Herfried,

I mean this one that I made some days ago for OHM, this sample is complete
and in the line of this newsgroup, I did not know there exist already one.
(Do not cry there are also with HKW in it).

http://groups.google.com/groups?selm...TNGP12.phx.gbl

It is valuechanged in stead of keypresses of course because you says always
that that is better
(not serious)

And that is the reason I saw your CType because I had used it there, I have
changed it now in my snippets in my HKW database, which is by the way a
tooltip sample in a datagrid that I changed for OHM.

Cor
Nov 20 '05 #9

P: n/a
* Cor Ligthert:
So why his and not mine?

I did not copied mine from his.


I didn't see that you posted a sample, and I would have had to change
it.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.