Results 1 to 7 of 7

Thread: [RESOLVED] Getting ColumnIndexes from DGV

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim minColumnIndex = selectedColumnIndices.Min()
    2. Dim maxColumnIndex = selectedColumnIndices.Max()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    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
  •  



Click Here to Expand Forum to Full Width