add new row and set selecteditem

Hi.

I'm programmatically adding new row to SfDataGrid. How can I set selection to this new row?

Regards,

Chris


1 Reply 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team August 2, 2021 01:50 PM UTC

Hi Chris,

Thank you for contacting Syncfusion Support.

You can set the selected item as new row by using SelectedIndex or SelectedItem property in SfDataGrid. Please refer the below code snippet, 
 
  private void Form1_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
{ 
            if (e.Action != System.Collections.Specialized.NotifyCollectionChangedAction.Add) return; 
 
            var recordIndex = e.NewStartingIndex; 
            if (recordIndex == -1) return; 
 
            //Apply selection by setting SelectedIndex as newly added Index 
            //this.sfDataGrid.SelectedIndex = recordIndex; 
 
            //or Apply selection by using SelectedItem 
            var rowIndex = sfDataGrid.TableControl.ResolveToRowIndex(recordIndex); 
            //get  the record by passing index in GetRecordAtRowIndex SelectionHelper method 
            var record = SelectionHelper.GetRecordAtRowIndex(this.sfDataGrid, rowIndex); 
            //set the selected item as record 
            this.sfDataGrid.SelectedItem = record; 
} 

For more information related to Selection, please refer the user guide documentation,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/selection#programmatic-selection 
 
Please let us know if you have any concerns in this. 

Regards, 
Vijayarasan S 


Marked as answer
Loader.
Up arrow icon