DataGrid Multiple SortExpressions

R

rn5a

A DataGrid control displays records from a SQL Server 2005 DB table.
The AllowSorting property of the DataGrid has been set to True & the
SortExpressions of the BoundColumns have been set to the different
columns so that users can sort the DataGrid by clicking the headers in
the DataGrid. The DataGrid can be sorted using all the headers in the
DataGrid except for the first column header.

<asp:DataGrid ID="dg1" AllowSorting="true" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="COL1">
<ItemTemplate>
<%# Container.DataItem("Col1") %>
</ItemTemplate>
</asp:TemplateColumn>

<asp:BoundColumn DataField="Col2" HeaderText="COL2"
SortExpression="Col2"/>
<asp:BoundColumn DataField="Col3" HeaderText="COL3"
SortExpression="Col3"/>
<asp:BoundColumn DataField="Col4" HeaderText="COL4"
SortExpression="Col4"/>

<asp:EditCommandColumn...../>
</Columns>
</asp:DataGrid>

In order to sort the DataGrid, I am binding the DataGrid to a DataView
so that I can use the DataView's Sort property. Unfortunately, the
above code sorts the DataGrid based on only one column i.e. the SQL
query would look something like this:

SELECT * FROM MyTable ORDER BY Col2 ASC

I need to sort the DataGrid based on multiple columns so that if 2
records under the Col2 column happen to be identical, the DataGrid
would then be sorted based on some other column, say, Col3 i.e. the SQL
query should look something like this:

SELECT * FROM MyTable ORDER BY Col2 ASC, Col3 DESC

Is there anyway by which the DataGrid can be sorted based on 2 or more
sort expressions?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top