So if you want to sort out the record by using a ASP.net GridView you have to do 2 thing for
ASPX
<asp:GridView ID="GVReport" runat="server" GridLines="Vertical"
AutoGenerateColumns="False" PageSize="20"
OnSorting="GVReport_Sorting">
<asp:TemplateField HeaderText="Approved By" SortExpression="Approved">
<ItemTemplate>
<%# Eval("Approved") %>
</ItemTemplate>
</asp:GridView>
If the AutoGenerateColumns="True" then no need to define for sort columns it automatically bind with each columns, and If you are using
TemplateField then you need to mention what columns you want to sort out , one thing make sure that in SortExpression it should be the bind column name.
OnSorting We need to define a function that in our server side code.
So on server side you need to do the following steps , I am not going through the database connectivity and Dataset etc .. I assume that you know all these stuff
protected void GVReport_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
DataSet ds = new DataSet();
ds = getDataSet();
DataView dataView = default(DataView);
dataView = new DataView(ds.Tables(0));
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortExpression);
GVReport.DataSource = dataView;
GVReport.DataBind();
}
private string ConvertSortDirectionToSql(string sortExpression)
{
if (ViewState("GVReport_" + sortExpression) == null | ViewState("GVReport_" + sortExpression) == "DESC") {
ViewState("GVReport_" + sortExpression) = "ASC";
return "ASC";
} else {
ViewState("GVReport_" + sortExpression) = "ASC";
ViewState("GVReport_" + sortExpression) = "DESC";
return "DESC";
}
}
So as you can see the ConvertSortDirectionToSql function is what is doing If the ViewState is null It apply Ascending order other wise descending
ASPX
<asp:GridView ID="GVReport" runat="server" GridLines="Vertical"
AutoGenerateColumns="False" PageSize="20"
OnSorting="GVReport_Sorting">
<asp:TemplateField HeaderText="Approved By" SortExpression="Approved">
<ItemTemplate>
<%# Eval("Approved") %>
</ItemTemplate>
</asp:GridView>
If the AutoGenerateColumns="True" then no need to define for sort columns it automatically bind with each columns, and If you are using
TemplateField then you need to mention what columns you want to sort out , one thing make sure that in SortExpression it should be the bind column name.
OnSorting We need to define a function that in our server side code.
So on server side you need to do the following steps , I am not going through the database connectivity and Dataset etc .. I assume that you know all these stuff
protected void GVReport_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
DataSet ds = new DataSet();
ds = getDataSet();
DataView dataView = default(DataView);
dataView = new DataView(ds.Tables(0));
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortExpression);
GVReport.DataSource = dataView;
GVReport.DataBind();
}
private string ConvertSortDirectionToSql(string sortExpression)
{
if (ViewState("GVReport_" + sortExpression) == null | ViewState("GVReport_" + sortExpression) == "DESC") {
ViewState("GVReport_" + sortExpression) = "ASC";
return "ASC";
} else {
ViewState("GVReport_" + sortExpression) = "ASC";
ViewState("GVReport_" + sortExpression) = "DESC";
return "DESC";
}
}
So as you can see the ConvertSortDirectionToSql function is what is doing If the ViewState is null It apply Ascending order other wise descending
No comments:
Post a Comment