Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of selected rows in a DataGridView

How to count the number of selected rows in a DataGridView?

Let's say I highlighted 5 rows, how can I show it in message box?

Please help I'm using WinForms in C#!

like image 723
NickSharp Avatar asked Sep 19 '25 06:09

NickSharp


1 Answers

you need to set YourGridView.MultiSelect=true; MultiSelect When the MultiSelect property is set to true, multiple elements (cells, rows, or columns) can be selected in the DataGridView control. To select multiple elements, the user can hold down the CTRL key while clicking the elements to select. Consecutive elements can be selected by clicking the first element to select and then, while holding down the SHIFT key, clicking the last element to select.

then you can use SelectRows.Count property SelectedRows

MessageBox.Show(yourDataGridView.SelectedRows.Count.ToString());
like image 168
Ravi Gadag Avatar answered Sep 21 '25 21:09

Ravi Gadag