Category / Section
How to move a current cell focus to the first cell of the add new row in WinForms GridGroupingControl?
2 mins read
Adding new record
By default, the focus will be set to the newly added row when the record is added by using AddNewRecordRow. In order to set focus to the AddNewRecordRow after a new record is added, SourceListRecordChanged and TableControlCurrentCellKeyDown events can be used. In SourceListRecordChanged event, LastChangedRecord property can be used to avoid the focus moving to newly added row and TableControlCurrentCellKeyDown event, SetCurrent() method can be used to set the first column as CurrentCell.
C#
//Event Triggering to avoid last focus if new record is added this.gridGroupingControl1.SourceListRecordChanged += GridGroupingControl_SourceListRecordChanged; //Event customization private void GridGroupingControl_SourceListRecordChanged(object sender,RecordChangedEventArgs e) { if (e.Action == RecordChangedType.Added) { //To avoid the last focus this.gridGroupingControl1.Table.LastChangedRecord = null; } } //Event Triggering to set cursor on current cell this.gridGroupingControl1.TableControlCurrentCellKeyDown += GridGroupingControl_TableControlCurrentCellKeyDown; //Event Customization private void GridGroupingControl_TableControlCurrentCellKeyDown(object sender,GridTableControlKeyEventArgs e) { { if (e.Inner.KeyCode == Keys.Enter && this.gridGroupingControl1.Table.CurrentRecord is AddNewRecord) { //To set focus for first cell of AddNewRecordRow provide first column name this.gridGroupingControl1.Table.AddNewRecord.SetCurrent("Column_Name"); } } }
VB
'Event Triggering to avoid last focus if new record is added AddHandler Me.gridGroupingControl1.SourceListRecordChanged, AddressOf GridGroupingControl_SourceListRecordChanged 'Event customization Private Sub GridGroupingControl_SourceListRecordChanged(ByVal sender As Object, ByVal e As RecordChangedEventArgs) If e.Action = RecordChangedType.Added Then 'To avoid the last focus Me.gridGroupingControl1.Table.LastChangedRecord = Nothing End If End Sub 'Event Triggering to set cursor on current cell AddHandler Me.gridGroupingControl1.TableControlCurrentCellKeyDown, AddressOf GridGroupingControl_TableControlCurrentCellKeyDown 'Event Customization Private Sub GridGroupingControl_TableControlCurrentCellKeyDown(ByVal sender As Object, ByVal e As GridTableControlKeyEventArgs) If e.Inner.KeyCode = Keys.Enter AndAlso TypeOf Me.gridGroupingControl1.Table.CurrentRecord Is AddNewRecord Then 'To set focus for first cell of AddNewRecordRow provide first column name Me.gridGroupingControl1.Table.AddNewRecord.SetCurrent("Column_Name") End If End Sub
Screenshot
Samples: