471,602 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,602 software developers and data experts.

How to trigger the keydown event in datagrid control ?

I would like to use datagrid's keydown event to capture the "Ctrl+D" when
users press this key. But it did not work. What did I miss? (I have already
set the form keypreview to true)

Private Sub grdInCategory_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles grdInCategory.KeyDown
If e.Control And e.KeyCode = Keys.D Then
If MsgBox("Are you sure to delete the record ?",
MsgBoxStyle.YesNo) = MsgBoxResult.No Then Exit Sub
If (Me.BindingContext(Me.objdsInCategory, "InCategory").Count >
0) Then
Me.BindingContext(Me.objdsInCategory,
"InCategory").RemoveAt(Me.BindingContext(Me.objdsI nCategory,
"InCategory").Position)
End If
End If
End Sub

Thank you
John Huang
Nov 21 '05 #1
2 5987
John,

Everything you always wanted to know from the datagrid
http://64.78.52.104/FAQ/WinForms/default.asp#44

I hope this helps,

Cor
Nov 21 '05 #2
Hi,

You need to create an inherited datagrid for that. Here is the code
for one that will raise an event to let you the user is about to delete a
row. Also prevents the user from resizing a row or column.

Public Class ConfirmDeleteDataGrid

Inherits DataGrid

Public Event DeletedRow(ByVal sender As Object, ByVal e As EventArgs)

Private Const WM_KEYDOWN = &H100

Public Overrides Function PreProcessMessage(ByRef msg As
System.Windows.Forms.Message) As Boolean

Dim keyCode As Keys = CType((msg.WParam.ToInt32 And Keys.KeyCode), Keys)

If msg.Msg = WM_KEYDOWN And keyCode = Keys.Delete Then

If MessageBox.Show("Delete This Row?", "Confirm Delete", _

MessageBoxButtons.YesNo) = DialogResult.No Then

Return True

Else

RaiseEvent DeletedRow(Me, New EventArgs)

End If

End If

Return MyBase.PreProcessMessage(msg)

End Function

Protected Overrides Function ProcessDialogKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean

Dim pt As Point

Dim hti As DataGrid.HitTestInfo

pt = Me.PointToClient(Cursor.Position)

hti = Me.HitTest(pt)

If keyData = Keys.Delete Then

If hti.Type = Me.HitTestType.RowHeader Then

If MessageBox.Show("Delete this row?", "Confirm Delete", _

MessageBoxButtons.YesNo) = DialogResult.No Then

Return True

Else

RaiseEvent DeletedRow(Me, New EventArgs)

End If

End If

End If

Return MyBase.ProcessDialogKey(keyData)

End Function

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)

Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseDown(e)

End Sub

Public Sub New()

Trace.WriteLine(Me.VertScrollBar.Visible.ToString)

End Sub

Protected Overrides Sub OnMouseMove(ByVal e As
System.Windows.Forms.MouseEventArgs)

Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseMove(e)

End Sub
End Class

Ken

-------------------------------
"John Huang" <John Hu***@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
I would like to use datagrid's keydown event to capture the "Ctrl+D" when
users press this key. But it did not work. What did I miss? (I have already
set the form keypreview to true)

Private Sub grdInCategory_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles grdInCategory.KeyDown
If e.Control And e.KeyCode = Keys.D Then
If MsgBox("Are you sure to delete the record ?",
MsgBoxStyle.YesNo) = MsgBoxResult.No Then Exit Sub
If (Me.BindingContext(Me.objdsInCategory, "InCategory").Count >
0) Then
Me.BindingContext(Me.objdsInCategory,
"InCategory").RemoveAt(Me.BindingContext(Me.objdsI nCategory,
"InCategory").Position)
End If
End If
End Sub

Thank you
John Huang
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Mike M | last post: by
3 posts views Thread by bardo | last post: by
4 posts views Thread by Tony | last post: by
1 post views Thread by fripper | last post: by
3 posts views Thread by MLM450 | last post: by
2 posts views Thread by Tony Johansson | last post: by
1 post views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.