page sort query by selection

I am trying to sort a query in an page.
the page contains a grid (peopleid, contactname, contactcompany), populated by a query against the database, the grid is sortable by lastname, company.(clicking the header)
I create a button on the page that will refresh an other query that is filtered by the contactid selected in my grid (all the selected contactid separated by a comma are stored in a label)
see code 1
The second query looks like (see code 2)

I would like the system to order the second query in the same order as they are selected in the grid. So if by example, I order my grid by company and then select each people seprately, the second query result will keep the order of selection.
By the way, the system actually store each peopleid in the lblpeopleidtxt correctly, but fail to keep this order in the second query.

Thanks for your help
code 1
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' StringBuilder object 
        Dim str As New StringBuilder()
        Dim first As Boolean = True
        For i As Integer = 0 To GridView1.Rows.Count - 1
            ' Select the checkboxes from the GridView control 
            Dim row As GridViewRow = GridView1.Rows(i)
            Dim isChecked As Boolean = DirectCast(row.FindControl("chkSelect"), CheckBox).Checked
            If isChecked Then
                If first Then
                    first = False
                End If
                ' Column 2 is the name column 
            End If
        ' prints out the result 
        ' Response.Write(str.ToString())
        lblpeopleid.Text = str.ToString
    End Sub
code 2: the query

Open in new window

Who is Participating?
talentiumConnect With a Mentor Author Commented:
Thanks for reply,
really interresting answer, As I use a datalist to show the result of the query (code 2)
the idea could be the same as yours with something like keeping the sorting of gridview1 in the
datalist1 based on the query selectedpeopleshortlist (see code of datalist 1 below)

I need some help in the syntax, this could probably be done in the page load like in the code snippet,
but when I try this I receive a msg that
'Sort' si not a member of 'System.Web.UI.webcontrols.Datalist'

code of the datalist 1
<asp:DataList ID="DataList1" runat="server" DataSourceID="selectedpeopleshortlist" RepeatLayout="Flow"
            ShowFooter="False" ShowHeader="False" Width="95%">
code for page load
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
DataList1.Sort = (Gridview1.SortExpression, GridView1.SortDirection)
  End Sub

Open in new window

In the code that binds GridView2, you can add this bit of code after binding the data to it.

GridView2.Sort(Gridview1.SortExpression, GridView1.SortDirection)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.