Articles in this section

How to Prevent Excess Row Selection in WinForms DataGrid?

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;
     }
}

PreventSelection

Take a moment to peruse the WinForms DataGrid - Selection documentation, to learn more about selection with examples.


View sample in GitHub


 Conclusion


I hope you enjoyed learning how to prevent excess row selection in WinForms DataGrid.

You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms DataGrid example to understand how to create and manipulate data.


For current customers, check out our components from the License and Downloads page. If you are new to Syncfusion, try our 30-day free trial to check out our other controls.


Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied