Setting up the DataGrid
First, there are several things that must happen to a DataGrid before you can sort -
-
You must turn AutoGenerateColumns on (=true). This means that you will lose any special columns and lose the ability to edit the data. It is a wise idea to create one page for viewing and another for editing.
-
You must add the attribute AllowSorting and set it to true. This makes each column header a link that will reload the page when clicked.
-
You must add the attribute OnSortCommand and set it to the method that will sort the data (in this case dg_sort).
-
You must add the method dg_sort.
We will be using the same page that we have been using for the whole series to view the data. See part 6 for a complete listing of that page. After making these changes, your DataGrid will look like this -
<asp:DataGrid id="dg" runat="server" Bordercolor="black" gridlines="vertical" font-names="Arial" font-size="10pt" HeaderStyle-BackColor="#FFCC00" ItemStyle-BackColor="#FFCC66" AlternatingItemStyle-BackColor="#FFFFFF" AutoGenerateColumns="True" OnEditCommand="dg_edit" OnCancelCommand="dg_cancel" OnUpdateCommand="dg_update" OnSortCommand="dg_sort" AllowSorting="True" > </asp:dataGrid> |
(The changes are in red.)
Here is a picture of what the DataGrid should look like -