|
-
Aug 2nd, 2010, 05:42 PM
#1
Thread Starter
Hyperactive Member
[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
It was much easier in VB6, but I am now liking Vb.Net alot more. 
-
Aug 2nd, 2010, 07:48 PM
#2
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.
-
Aug 3rd, 2010, 04:29 PM
#3
Thread Starter
Hyperactive Member
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
It was much easier in VB6, but I am now liking Vb.Net alot more. 
-
Aug 3rd, 2010, 06:20 PM
#4
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()
-
Aug 4th, 2010, 05:02 AM
#5
Thread Starter
Hyperactive Member
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
It was much easier in VB6, but I am now liking Vb.Net alot more. 
-
Aug 4th, 2010, 05:07 AM
#6
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
-
Aug 4th, 2010, 08:22 AM
#7
Thread Starter
Hyperactive Member
Re: [RESOLVED] Getting ColumnIndexes from DGV
Again many thanks.
Computerman
It was much easier in VB6, but I am now liking Vb.Net alot more. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|