Articles in this section

How to make WinForms DataGrid to allow only a specific number of items to be selected?

WinForms DataGrid (SfDataGrid) allows you to select multiple rows or cells by setting the SelectionMode property as Multiple. You can make SfDataGrid to allow only a specific number of items to be selected by using the SfDataGrid.SelectionChanging and SfDataGrid.CurrentCellActivating events.

sfDataGrid1.SelectionChanging += OnSfDataGrid1_SelectionChanging;
sfDataGrid1.CurrentCellActivating += OnSfDataGrid1_CurrentCellActivating;
 
private void OnSfDataGrid1_CurrentCellActivating(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellActivatingEventArgs e)
{
    if ((sender as SfDataGrid).SelectedItems.Count == 2 && !(sender as SfDataGrid).SelectedItems.Contains(e.DataRow.RowData))
        e.Cancel = true;
}
 
private void OnSfDataGrid1_SelectionChanging(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangingEventArgs e)
{
    if (e.RemovedItems.Count ==0 && (sender as SfDataGrid).SelectedItems.Count == 2)
        e.Cancel = true;
}

 

Take a moment to peruse the documentation, where you can find about selection in SfDataGrid, with code examples.

Please refer this link to know about the essential features of Syncfusion WPF DataGrid.

You can download the example from GitHub.


Conclusion

I hope you enjoyed learning about how to make WinForms DataGrid to allow only a specific number of items to be selected.

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

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

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-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