We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How can i catch a return key from datagridtextboxcolumn

Hi, Please I need help. I created a form that allows me searching customers by name. after display customers list in datagrid, i want allow user to select one customer by pressing return key (chr(13)). for grid setup, i tried the following code : 'Configuration de la grille Private Sub DataGridSetup() Dim ts As New DataGridTableStyle() Dim cs As New DataGridTextBoxColumn() AddHandler cs.TextBox.KeyPress, AddressOf CellKeyPress DataGrid1.CaptionText = "Résultat" ts.MappingName = "mycli" cs.MappingName = "c_cli" cs.HeaderText = "Code" cs.Alignment = HorizontalAlignment.Center ts.GridColumnStyles.Add(cs) cs = New DataGridTextBoxColumn() cs.MappingName = "N_cli" cs.HeaderText = "Nom/Raison sociale" ts.GridColumnStyles.Add(cs) DataGrid1.TableStyles.Clear() DataGrid1.TableStyles.Add(ts) End Sub and i added handler to datagridtextboxcolumn, for detecting the keypress events as follows : 'Ajout d'un évènement aux cellules Public Sub CellKeyPress(ByVal Sender As Object, ByVal e As KeyPressEventArgs) Dim dt As DataTable = ds.Tables("mycli") Dim p As Integer CodeClient = Space(11) If e.KeyChar = Chr(13) Then p = Me.BindingContext(ds, "mycli").Position CodeClient = dt.Rows(p)("c_cli") Me.Close() End If End Sub So after writing this code, i can select one customer only after pressing SHIFT+RETURN. please can you explain me way i must first press SHIFT. Thank you very match. Intibnin...

1 Reply

AD Administrator Syncfusion Team January 8, 2003 07:23 AM UTC

I think that the grid must be catching the enter key and not passing it onto the embedded textbox control. You can catch this enter key without using columnstyles by deriving the DataGrid and overriding ProcessKeyPreview.
Public Class MyDataGrid
	Inherits DataGrid
    
	Private Const WM_KEYDOWN As Integer = &H100
	Private Const WM_KEYUP As Integer = &H101
    
    
	Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) As Boolean
		Dim keyCode As Keys = CType(m.WParam.ToInt32(), Keys) And Keys.KeyCode
		If m.Msg = WM_KEYDOWN OrElse m.Msg = WM_KEYUP AndAlso keyCode = Keys.Enter Then
			Console.WriteLine("grid Keys.Enter")
			' return false; 'ignore
		End If 
		Return True
	End Function 'ProcessKeyPreview
End Class 'MyDataGrid

Loader.
Live Chat Icon For mobile
Up arrow icon