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

Keypress event inside datagrid cell

P: 2
Ok a couple things I've got going: On the DataGrid1_CurrentCellChanged I add a button control to the datagridtextbox. This occurs when a mouse click happens inside the cell or the cursor enters the cell. I'm trying to figure out how to capture the Enter key so to run the buttoncontrol click function. I can get this to work when the datagrid only is activated (by clicking on the whitespace of the grid) but of course it won't work when the cursor is activated. I either need to capture the keypress event while a cell is activated so it can run the button control function or after I add the button control to the datagridtextbox somehow select the whitespace so the datagrid.keypress event will run.
FYI - I'm not sure how to implement this solution or if it will work for what I need: Override ProcessCmdKey

Code:
Expand|Select|Wrap|Line Numbers
  1. Public Sub FormatCellControl()
  2.         'Create the button control to be added and set its properties
  3.         ButtonControl = New Button
  4.         ButtonControl.Cursor = System.Windows.Forms.Cursors.Arrow
  5.         ButtonControl.Dock = DockStyle.Fill
  6.         ButtonControl.SendToBack()
  7.         Dim CurrentColumn As Int16 = DataGrid1.CurrentCell.ColumnNumber
  8.         Select Case CurrentColumn
  9.         Case 2
  10.         DatagridTextBox = CType(DataGrid1.TableStyles(0).GridColumnStyles(2), DataGridTextBoxColumn)
  11.         DatagridTextBox.TextBox.Controls.Add(ButtonControl)
  12.         ButtonControl.BringToFront()
  13. End Sub
  14.  
  15. Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
  16.     FormatCellControl()
  17. End Sub
  18. Private Sub DataGrid1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DataGrid1.KeyPress
  19.     If Datagrid1.CurrentCell.ColumnNumber = 2 Then
  20.         ButtonClick()
  21.     End if
  22. End Sub
  23. Private Sub ButtonClick()
  24.     Dim ChildFrm As New Form2()
  25.         ChildFrm.ShowDialog()
  26. End Sub
Sep 27 '06 #1
Share this Question
Share on Google+
2 Replies


P: 3
Hi chatelain,
Thanks a lot. your code solved my problem and now I solve urs :)
To get the click event of the button in the cell (that u selects), please add declare the following italic line (pointed by arrow) line as
---> Dim ButtonControl As New Button
ButtonControl.Text = "Path"
ButtonControl.Width = 50
ButtonControl.Cursor = System.Windows.Forms.Cursors.Arrow
ButtonControl.Dock = DockStyle.Right
ButtonControl.SendToBack()
Dim CurrentColumn As Int16 = myDataGrid.CurrentCell.ColumnNumber
Dim DataGridTextBox = CType(myDataGrid.TableStyles(0).GridColumnStyles(5 ), DataGridTextBoxColumn)
DataGridTextBox.TextBox.Controls.Add(ButtonControl )
ButtonControl.BringToFront()

as a public member of the class, as

Public WithEvents ButtonControl As New Button

Now add ur handler as

Private Sub ButtonControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonControl.Click
MsgBox("test")
End Sub

Enjoy :)
Regards

Ok a couple things I've got going: On the DataGrid1_CurrentCellChanged I add a button control to the datagridtextbox. This occurs when a mouse click happens inside the cell or the cursor enters the cell. I'm trying to figure out how to capture the Enter key so to run the buttoncontrol click function. I can get this to work when the datagrid only is activated (by clicking on the whitespace of the grid) but of course it won't work when the cursor is activated. I either need to capture the keypress event while a cell is activated so it can run the button control function or after I add the button control to the datagridtextbox somehow select the whitespace so the datagrid.keypress event will run.
FYI - I'm not sure how to implement this solution or if it will work for what I need: Override ProcessCmdKey

Code:
Expand|Select|Wrap|Line Numbers
  1. Public Sub FormatCellControl()
  2.         'Create the button control to be added and set its properties
  3.         ButtonControl = New Button
  4.         ButtonControl.Cursor = System.Windows.Forms.Cursors.Arrow
  5.         ButtonControl.Dock = DockStyle.Fill
  6.         ButtonControl.SendToBack()
  7.         Dim CurrentColumn As Int16 = DataGrid1.CurrentCell.ColumnNumber
  8.         Select Case CurrentColumn
  9.         Case 2
  10.         DatagridTextBox = CType(DataGrid1.TableStyles(0).GridColumnStyles(2), DataGridTextBoxColumn)
  11.         DatagridTextBox.TextBox.Controls.Add(ButtonControl)
  12.         ButtonControl.BringToFront()
  13. End Sub
  14.  
  15. Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
  16.     FormatCellControl()
  17. End Sub
  18. Private Sub DataGrid1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles DataGrid1.KeyPress
  19.     If Datagrid1.CurrentCell.ColumnNumber = 2 Then
  20.         ButtonClick()
  21.     End if
  22. End Sub
  23. Private Sub ButtonClick()
  24.     Dim ChildFrm As New Form2()
  25.         ChildFrm.ShowDialog()
  26. End Sub
Jul 30 '07 #2

P: 3
Hi All,
I am using datagrid control in my window application. I am designing the grid by a table and table style to add extra controls in the cells like button and drop down.
Now, I have everything ok, But it want to disable the navigation in the datagrid cells by arrow keys. I dont want to change the cell by the arrows key as the Tab key does.
Is there anybody to help me out.
Thnx in advance.
Mos
Aug 1 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.