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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get the selected record when Enter key is pressed in WinForms DataGrid (SfDataGrid)?

Platform: WinForms |
Control: SfDataGrid |
Published Date: August 31, 2018 |
Last Revised Date: January 13, 2020
Tags: selection

Show the selected record while pressing the enter key

In SfDataGrid, you can retrieve and process the selected record when pressing Enter key on the current cell using the SfDtaGrid.CurrentCellKeyDown event.

The following code shows how to get and process the selected record when pressing Enter key on a record cell.

C#

this.sfDataGrid.CurrentCellKeyDown += sfDataGrid_CurrentCellKeyDown;
 
void sfDataGrid_CurrentCellKeyDown(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellKeyEventArgs e)
{
    if(e.KeyEventArgs.KeyCode == Keys.Enter)
    {
        if (this.sfDataGrid.SelectedItem != null)
        {
            var selectedRecord = this.sfDataGrid.SelectedItem as OrderInfo;
            var orderDate = selectedRecord.OrderDate;
 
            // Do your customization here.
        }
        e.KeyEventArgs.SuppressKeyPress = true;
    }
}

VB

AddHandler Me.sfDataGrid.CurrentCellKeyDown, AddressOf sfDataGrid_CurrentCellKeyDown
 
Private Sub sfDataGrid_CurrentCellKeyDown(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.CurrentCellKeyEventArgs)
 If e.KeyEventArgs.KeyCode = Keys.Enter Then
  If Me.sfDataGrid.SelectedItem IsNot Nothing Then
   Dim selectedRecord = TryCast(Me.sfDataGrid.SelectedItem, OrderInfo)
   Dim orderDate = selectedRecord.OrderDate
 
   ' Do your customization here.
  End If
  e.KeyEventArgs.SuppressKeyPress = True
 End If
End Sub

Samples:

C#: CurrentRecord_CS

VB: CurrentRecord_VB

The following code shows how to get and process the group records when pressing Enter key on a group caption.

C#

this.sfDataGrid.CurrentCellKeyDown += sfDataGrid_CurrentCellKeyDown;
void sfDataGrid_CurrentCellKeyDown(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellKeyEventArgs e)
{
    if (e.KeyEventArgs.KeyCode == Keys.Enter && this.sfDataGrid.SelectedNodeEntries.Count > 0)
    {
        var selectedEntry = this.sfDataGrid.SelectedNodeEntries[this.sfDataGrid.SelectedNodeEntries.Count - 1];
        if (selectedEntry != null && selectedEntry.IsCaptionRow)
        {
            var group = selectedEntry.NodeEntry as Group;
            var groupRecordEntry = group.Details as GroupRecordEntry;
            var date = (groupRecordEntry.UnfilteredRecords[0] as OrderInfo).OrderDate;
            var record = (groupRecordEntry.Records[0] as RecordEntry).Data as OrderInfo;
            var orderDate = record.OrderDate;
            //Do your customizations here.
        }
        e.KeyEventArgs.SuppressKeyPress = true;
    }
}

VB

AddHandler Me.sfDataGrid.CurrentCellKeyDown, AddressOf sfDataGrid_CurrentCellKeyDown
 
Private Sub sfDataGrid_CurrentCellKeyDown(ByVal sender As Object, ByVal e As Syncfusion.WinForms.DataGrid.Events.CurrentCellKeyEventArgs)
 If e.KeyEventArgs.KeyCode = Keys.Enter AndAlso Me.sfDataGrid.SelectedNodeEntries.Count > 0 Then
  Dim selectedEntry = Me.sfDataGrid.SelectedNodeEntries(Me.sfDataGrid.SelectedNodeEntries.Count - 1)
  If selectedEntry IsNot Nothing AndAlso selectedEntry.IsCaptionRow Then
   Dim group = TryCast(selectedEntry.NodeEntry, Group)
   Dim groupRecordEntry = TryCast(group.Details, GroupRecordEntry)
   Dim [date] = (TryCast(groupRecordEntry.UnfilteredRecords(0), OrderInfo)).OrderDate
   Dim record = TryCast((TryCast(groupRecordEntry.Records(0), RecordEntry)).Data, OrderInfo)
   Dim orderDate = record.OrderDate
   'Do your customizations here.
  End If
  e.KeyEventArgs.SuppressKeyPress = True
 End If
End Sub

Samples:

C# : GroupRecords_CS

VB : GroupRecords_VB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile