In WinForms DataGrid (SfDataGrid), limiting selection to a maximum of two can be achieved by handling the SelectionChanging event. Within this event, logic can be applied to evaluate the counts of SelectedItems, RemovedItems, and AddedItems. If the total number of selected rows exceeds two, the selection process can be cancelled by setting e.cancel to true.
//Event subscription
sfDataGrid1.SelectionChanging += OnSelectionChanging;
//Event customization
private void OnSelectionChanging(object sender, SelectionChangingEventArgs e)
{
var dataGrid = (sender as SfDataGrid);
if (dataGrid != null && (dataGrid.SelectedItems.Count + e.AddedItems.Count - e.RemovedItems.Count > 2))
{
// Cancel the selection above 2 rows
e.Cancel = true;
}
}
Take a moment to peruse the WinForms DataGrid - Selection documentation, to learn more about selection with examples.