|
-
Nov 17th, 2012, 10:31 PM
#1
Thread Starter
Member
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.")
-
Nov 17th, 2012, 10:59 PM
#2
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.
-
Nov 17th, 2012, 11:27 PM
#3
Thread Starter
Member
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?
-
Nov 18th, 2012, 12:12 AM
#4
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
-
Jul 7th, 2014, 05:02 AM
#5
Addicted Member
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
-
Jul 7th, 2014, 05:48 AM
#6
Re: order of selected rows of datagridview
 Originally Posted by kriswork
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]
-
Jul 7th, 2014, 06:02 AM
#7
Addicted Member
Re: order of selected rows of datagridview
 Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|