Your executed command to your database should be something like the following. I am not gonna show you how to execute a command to the database cause I figure you got that part taken care of.
SELECT COLUMN1, COLUMN2 FROM MYTABLE
If executed correctly and bound correctly you will need to add the following columns to the aspx DataGrid object
<asp:DataGrid ID="myDataGrid" Runat="server" AutoGenerateColumns="False" AllowSorting="True">
<Columns>
<asp:BoundColumn HeaderText="Column 1" DataField="COLUMN1" SortExpression="COLUMN1" ReadOnly="True" />
<asp:BoundColumn HeaderText="Column 2" DataField="COLUMN2" SortExpression="COLUMN2" ReadOnly="True" />
</Columns>
</asp:DataGrid>
Next is the sorting event handler. My example uses a DataTable because thats what I used when I wrote my code...sorry
Protected Sub Sort(ByVal Source As Object, ByVal e As DataGridSortCommandEventArgs) Handles myDataGrid.SortCommand
Dim Column As String = myDataTable.DefaultView.Sort.Replace(" asc", "").Replace(" desc", "")
Dim Order As String = " asc"
If e.SortExpression = Column Then
If myDataTable.DefaultView.Sort.EndsWith(" asc") Then
Order = " desc"
myDataTable.DefaultView.Sort = e.SortExpression + Order
ElseIf myDataTable.DefaultView.Sort.EndsWith(" desc") Then
myDataTable.DefaultView.Sort = e.SortExpression + Order
Else
myDataTable.DefaultView.Sort = e.SortExpression + Order
End If
Else
myDataTable.DefaultView.Sort = e.SortExpression + Order
End If
myDataGrid.EditItemIndex = -1
BindData()
End Sub
I can not recall if you need to store the data source object in the session to avoid the sort order being lost in the post back call but you might if you find the sorting not carrying during pagination. This is a general way of getting bound columns to sort. I prefer using a DataTable instead of DataSource but thats me.