Does anyone know how to get the number of rows that are included in the total SelectedCells of a datagridview?
This is where the user actually selects multiple cells in multiple rows - not actually selecting the rows themselves.
I don't need to row indexes of the cells. What I need to get is the total number of rows included in the entire SelectedCells collection.

Appreciate any feed back.

This is untested, but off the top of my head seems reasonable:

rowCount = dgv.SelectedCells.OfType(Of DataGridViewCell)().Select(Function(x) x.RowIndex).Distinct().Count()

Essentially, each of the selected cells has a RowIndex property. You can use that to determine the count of rows and also any of the selected cell row indices if needed (which is probable).

Excellent. Thank you.

I did something similar until I received your post.
I actually just created a Datatable and then added each row index as I was looping thru the SelectedCells.
I then used LINQ to extract out the Distinct Values which then provided me with the count of row.

I will insert your code as test. If works I will use it as it's smaller amount of code and a bit more efficent.

thanx again....

deceptikon,

I could not use your suggestion as the SelectedCells does not include an "Of Type" property. Not sure how to transform what you provided into a valid LINQ query.

OfType is a LINQ extension method. You'd need to add System.Linq to your Imports statements and make sure that the appropriate assemblies are referenced.

Added System.Linq....received error below:

Error 105 Overload resolution failed because no accessible 'Select' can be called with these arguments:
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of System.Windows.Forms.DataGridViewCell, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Nested function does not have the same signature as delegate 'System.Func(Of System.Windows.Forms.DataGridViewCell, Integer, TResult)'.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of System.Windows.Forms.DataGridViewCell, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of System.Windows.Forms.DataGridViewCell, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Lambda parameter 'x' hides a variable in an enclosing block, a previously defined range variable, or an implicitly declared variable in a query expression.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of System.Windows.Forms.DataGridViewCell, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.

This article has been dead for over six months. Start a new discussion instead.