Hello
I'm trying to sort my gridview by a particular colum.
My gridview has a column which is 'Sort Order' and they are numbered 1,2,3 etc.
Now I want to be able to default sort that column in ascending order.
Would anyone possibly be able to help?
Here is my ASP.net code:
<asp:GridView ID="rd" runat="Server" OnPageIndexChanging="pageddata"
AllowPaging="True" AutoGenerateColumns="False" PageSize="30" CellPadding="4"
ForeColor="#333333" GridLines="None" AllowSorting="true" OnSorting="onSort">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField HeaderText="DataField">
<ItemTemplate>
<asp:Label ID="lblDataField" runat="server" Text='<%#Eval("DataField") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDataField" runat="server" ReadOnly="true" Visible="true" Text='<%#Eval("DataField") %>'></asp:TextBox>
<%-- Visible=false to hide textbox, ReadOnly=true to make textbox ReadOnly --%>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header">
<ItemTemplate>
<asp:Label ID="lblHeader" runat="server" Text='<%#Eval("Header") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtHeader" runat="server" Text='<%#Eval("Header") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UseDefaults">
<ItemTemplate>
<asp:Label ID="lblUseDefaults" runat="server" Text='<%#Eval("UseDefaults") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUseDefaults" runat="server" Text='<%#Eval("UseDefaults") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<%-- Taking Sort Order from XML file --%>
<asp:TemplateField HeaderText="Sort Order" SortExpression="sortOrder">
<ItemTemplate>
<asp:Label ID="lblSortOrder" runat="server" Text='<%#Eval("SortOrder") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ButtonType="Button" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
The DataBind:
void binddata()
{
DataSet rdawTest = new DataSet();
rdawTest.ReadXml(Server.MapPath(@"xml\" + Request["Gradebook"] + ".xml"));
foreach (DataColumn da in rdawTest.Tables[0].Columns)
{
da.ColumnMapping = MappingType.Attribute;
}
rd.DataSource = rdawTest;
rd.DataBind();
}
Thank you in advance.