Results 1 to 7 of 7

Thread: order of selected rows of datagridview

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    43

    order of selected rows of datagridview

    I manually select certain rows of a datagridview then click a button to output those rows to a file. But the order in which they are printed seems to be the reverse order from the order in which I selected them. Is this the expected behavior? Not a problem to loop backwards through the list, but I need to know if it will always be the case (note that selecting all rows using Control-A also seems to output them in the reverse order also).

    This is the code I'm using.

    Code:
                Dim sw As StreamWriter
    'code not shown here to set up sw
                Dim Arow As DataGridViewRow
                Dim nrow As Integer = 0
                Dim ndel As Integer = dgvLots.SelectedRows.Count
                For i as integer = 0 To ndel - 1
                    Arow = dgvLots.SelectedRows(i)
                    nrow += 1
                    OutputRow(sw, Arow)
                Next
                MessageBox.Show(nrow.ToString & " written.")

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

    Re: order of selected rows of datagridview

    The documentation doesn't say anything about the order of the items in the collection so you shouldn't make any assumptions about the order of the items in the collection. Apparently the current implementation is such that the items appear in the collection in the reverse order of their selection but there's no guarantee that that will always be the case. If the order is important to you then you should keep a record of that order yourself.
    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
    Member
    Join Date
    Jun 2012
    Posts
    43

    Re: order of selected rows of datagridview

    Wwll, what I want to do is output the selected rows in the order they appear in the datagridview, so I suppose the best way to do that would be just to loop through all the rows and output only those that have been selected?

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

    Re: order of selected rows of datagridview

    Each row has an Index property, so you can just order them by that:
    Code:
    For Each row In DataGridView1.SelectedRows.Cast(Of DataGridViewRow)().OrderBy(Function(dgvr) dgvr.Index)
        '...
    Next
    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
    Addicted Member
    Join Date
    Jul 2012
    Location
    Wiltshire, England
    Posts
    211

    Re: order of selected rows of datagridview

    if you do want to reverse the order of the selected items, you can use this code:

    Code:
            Dim byteRow_Index As Byte
    
            Dim byteSelected_Row_Index(dgCompounds.SelectedRows.Count - 1) As Byte
    
            For byteRow_Index = 0 To dgCompounds.SelectedRows.Count - 1
                byteSelected_Row_Index(byteRow_Index) = dgCompounds.SelectedRows(byteRow_Index).Index
            Next
    
            dgCompounds.ClearSelection()
    
            For byteRow_Index = 0 To byteSelected_Row_Index.Count - 1
                dgCompounds.Rows(byteSelected_Row_Index(byteRow_Index)).Selected = True
            Next

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

    Re: order of selected rows of datagridview

    Quote Originally Posted by kriswork View Post
    if you do want to reverse the order of the selected items, you can use this code:

    Code:
            Dim byteRow_Index As Byte
    
            Dim byteSelected_Row_Index(dgCompounds.SelectedRows.Count - 1) As Byte
    
            For byteRow_Index = 0 To dgCompounds.SelectedRows.Count - 1
                byteSelected_Row_Index(byteRow_Index) = dgCompounds.SelectedRows(byteRow_Index).Index
            Next
    
            dgCompounds.ClearSelection()
    
            For byteRow_Index = 0 To byteSelected_Row_Index.Count - 1
                dgCompounds.Rows(byteSelected_Row_Index(byteRow_Index)).Selected = True
            Next
    Or:
    Code:
    For Each row In DataGridView1.SelectedRows.Cast(Of DataGridViewRow)().OrderByDescending(Function(dgvr) dgvr.Index)
        '...
    Next
    [/QUOTE]
    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
    Addicted Member
    Join Date
    Jul 2012
    Location
    Wiltshire, England
    Posts
    211

    Re: order of selected rows of datagridview

    Quote Originally Posted by jmcilhinney View Post
    Or:
    Code:
    For Each row In DataGridView1.SelectedRows.Cast(Of DataGridViewRow)().OrderByDescending(Function(dgvr) dgvr.Index)
        '...
    Next
    [/QUOTE]

    My solution is much better because if I don't have to think too hard to understand my code ;-)

    Kris

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