[RESOLVED] Getting ColumnIndexes from DGV
A user selectes the following cells in the following order:
DGV.Rows(5).Cells(4) to DGV.Rows(5).Cells(2)
through to
DGV.Rows(2).Cells(4) to DGV.Rows(2).Cells(2)
What I need to record is the ColumnIndexes of the first selected row.
Any help would be appreciated.
Computerman :confused:
Re: Getting ColumnIndexes from DGV
The grid has a SelectedCells collection. You can loop through that and get the column and row indexes of each cell and do with them whatever you like.
Re: Getting ColumnIndexes from DGV
This is what I finally came up.
Code:
Dim selectedColumnIndices = From c In Me.MonthDGV.SelectedCells.Cast(Of DataGridViewCell)() Select c.ColumnIndex Distinct
Dim FirstColumnSelected As Integer = MonthDGV.SelectedCells(0).ColumnIndex
Dim LastColumnSelected As Integer = MonthDGV.SelectedCells(0).ColumnIndex
For i = 0 To selectedColumnIndices.Count - 1
If selectedColumnIndices(i) > LastColumnSelected Then
LastColumnSelected = selectedColumnIndices(i)
ElseIf selectedColumnIndices(i) < FirstColumnSelected Then
FirstColumnSelected = selectedColumnIndices(i)
End If
Next
Computerman :)
Re: [RESOLVED] Getting ColumnIndexes from DGV
If you're going to use LINQ anyway, why not use the Min and Max methods?
vb.net Code:
Dim minColumnIndex = selectedColumnIndices.Min()
Dim maxColumnIndex = selectedColumnIndices.Max()
Re: [RESOLVED] Getting ColumnIndexes from DGV
Many thanks for that. I am just starting to use LINQ so that it is very useful. Where is the best place to learn more about LINQ?
Computerman :)
Re: [RESOLVED] Getting ColumnIndexes from DGV
There's lots of information on LINQ all over the place but, whatever else you read, you should definitely go through this:
http://msdn.microsoft.com/en-us/vbasic/bb688088.aspx
Re: [RESOLVED] Getting ColumnIndexes from DGV
Again many thanks.
Computerman :)