I have a web application (with C#). I have a GridView and want to be able to sort its contents. I have added the tags
…
AllowSorting="True"
onsorting="MyGridView_Sorting">
and
asp:BoundField DataField="NAME" HeaderText="Name" SortExpression="NAME"
inside the GridView. I have implemented the MyGridView_Sorting method. Thing is: it does not work. Does nothing. The header text “Name” looks like an active link, but clicking produces no effect. Putting a break point inside
MyGridView_Sorting
shows that it actually never goes inside the function. What is wrong? What do I miss?
Thanks!!!
Sep
< asp:GridView ID="MyGridView"
runat="server"
CssClass="pvgrid"
Width="90%"
AutoGenerateColumns="false"
OnRowCommand="MyGridView_RowCommand"
AllowPaging="True"
PageSize="10"
AllowSorting="True"
onsorting="MyGridView_Sorting"
onpageindexchanging="MyGridView_PageIndexChanging" >
< Columns >
< asp:BoundField DataField="NAME" HeaderText="Name" SortExpression="NAME" />
< /Columns >
< /asp:GridView >
protected void MyGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
MyGridView.PageIndex = e.NewPageIndex;
MyGridView.DataBind();
}
protected void MyGridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = MyGridView.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " "+ ConvertSortDirectionToSql(e.SortDirection);
MyGridView.DataSource = dataView;
MyGridView.DataBind();
}
}
GridView delegates sorting to underlying DataSource. GridView does not perform sorting on its own, it just delegates. So you need to look into your DataSource for sorting. What is the DataSource ? What kind of Data does it display ? Are those custom objects coming from
ObjectDataSourceor you’re usingSqlDataSourceorLinqDataSource?