How do you sort a column in a datagrid?

burak

Centurion
Joined
Jun 17, 2003
Messages
127
Hello,

I have a datagrid as follows

<asp:datagrid id="dtgIncomplete" Runat="server" OnPageIndexChanged="PageActiveRecords" AllowPaging="True" PageSize="25" autogeneratecolumns="false" Width="537px" AllowSorting="True" OnSortCommand="SortCommand_OnClick">
<Columns>
<asp:BoundColumn HeaderText="VJC Ref#" SortExpression="JOB_ID" DataField="JOB_ID" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>

<asp:BoundColumn HeaderText="Company Job ID" SortExpression="EMPLOYERS_JOB_ID" DataField="EMPLOYERS_JOB_ID" HeaderStyle-HorizontalAlign="Center"
etc..
</Columns>
</asp:datagrid>

and here is my sort function

Sub SortCommand_OnClick(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dtgIncomplete.SortCommand

GetActiveJobs(dtgIncomplete.CurrentPageIndex, e.SortExpression)
End Sub


Sub GetActiveJobs(ByVal index As Integer, Optional ByVal strSort As String = "")

Dim Source As DataView = ds.Tables(0).DefaultView
If strSort = "" Then
Source.Sort = "job_id"
Else
Source.Sort = strSort
End If

'bind the data source
dtgIncomplete.DataSource = Source
dtgIncomplete.CurrentPageIndex = index
dtgIncomplete.PageSize = lstDisplay.SelectedValue
dtgIncomplete.DataBind()

End Sub

When I click on the header columns in the datagrid, it only does an ascending sort.

How can I have it do both ascending and descending?

Thank you,

Burak
 
You need a variable let's SortOrder = "ASC" then toggle it in GetActiveJobs() ...
if SortOrder = " ASC" then
SortOrder = " DESC"
'you get the idea....

then do this ...
Source.Sort = strSort & SortOrder
 
Back
Top