Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
have inserted DataGridView1 when more then 1 rows selected, I need the Index from Ascending Order.
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim selectedItems As DataGridViewSelectedRowCollection = DataGridView1.SelectedRows
        Debug.Print(DataGridView1.CurrentRow.Index)
        For Each selectedItem As DataGridViewRow In selectedItems
        Next
    End Sub
End Class


Output required : All the Selected Index should be in Ascending Order, which ever order it might be selected in grid in VB.net
Posted
Comments
Wombaticus 27-Jan-15 17:02pm    
A quick and dirty way of sorting anything in winforms applicataions is to add a listbnx, set visible=false and sorted=true, add the items to that, and let it sort them for you - then just read them back in order.
CHill60 27-Jan-15 19:34pm    
Dirty yes ... but hey ... it works! I have been known to do this in my time ;-)

1 solution

Create some data type which carries the data relevant to sorting (it could be the Value of just one columns, or something else, depending on your sorting criteria) plus — importantly — the index in the grid view at the moment.

Collect all instances of this data type from the grid view and put all these instances in the instance of the list System.Collections.Generic.List<T>, where T should be this data type:
https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^].

Then sort this list using your comparison criteria:
https://msdn.microsoft.com/en-us/library/w56d4y5z(v=vs.110).aspx[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900