Skip to content

SyncfusionExamples/How-to-prevent-the-user-from-selecting-more-than-a-specific-number-of-rows-in-a-WinForms-DataGrid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

How to prevent the user from selecting more than a specific number of rows in a 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;
     }
}

Prevent the selection

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

About

This demo shows how to prevent the user from selecting more than a specific number of rows in a WinForms DataGrid

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages