Thursday, August 27, 2009

Sorting and Paging a GridView


Sorting can be tricky. But maintaining the sort order on paging is trickier
Here is a sample code for that

DataBase used is Northwind.
Table used is Products.

The ASPX



<asp:GridView ID="GridView1" runat="server"
AllowPaging="true"
AllowSorting="true"
AutoGenerateColumns="false"
DataKeyNames="ProductID"
PageSize="10"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnSorting="GridView1_Sorting">
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="ProductID">
<ItemTemplate>
<asp:Label ID="lblProductID" runat="server"
Text='<%#Eval("ProductID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="ProductName">
<ItemTemplate>
<asp:Label ID="lblProductName" runat="server"
Text='<%#Eval("ProductName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Stock" SortExpression="UnitsInStock">
<ItemTemplate>
<asp:Label ID="lblUnitsInStock" runat="server"
Text='<%#Eval("UnitsInStock") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>



The ASPX.CS



private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
static private DataView dvProducts;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dvProducts = new DataView(GetProductsFromDataTable());
BindGridView();
}
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;

if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
dvProducts.Sort = sortExpression + DESCENDING;
BindGridView();
}
else
{
GridViewSortDirection = SortDirection.Ascending;
dvProducts.Sort = sortExpression + ASCENDING;
BindGridView();
}
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}

private void BindGridView()
{
GridView1.DataSource = dvProducts;
GridView1.DataBind();
}

public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;

return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}

private DataTable GetProductsFromDataTable()
{
DataTable dtProductsTemp = new DataTable();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString))
{
using (SqlDataAdapter adapProducts = new SqlDataAdapter("Select * from Products", conn))
{
adapProducts.Fill(dtProductsTemp);
}
}
return dtProductsTemp;
}



Extra namespace used



using System.Data.SqlClient;

Related Posts :



2 comments on "Sorting and Paging a GridView"

Add your comment. Please don't spam!
Subscribe in a Reader
十二月 on June 17, 2010 at 7:28 AM said...

I'm currently doing the search summary. It can be searched by priority, status, application or all. Well, your code(paging and sorting) runs well when it is used to search all the information. When I search by the priority, let say, there is 5 records in total, it shows the correct information, the problem is when I click the header to perform sorting, it shows more than 5 records, indeed, showing all the information in the database, by right, it should only show the 5 records in ascending or descending order when I click on the header.
Another problem is the paging. Let say, I search by the status, it contains 6 records, the paging size is set to be 5. So, page1 will show 5 records while page2 will only show 1 record. Lets don't talk about the sorting first, when I click on the page2, *pop*, all the information shows up, instead of just showing the 6 records. Same problem with the sorting part.
Any suggestion??

rakesh on December 3, 2010 at 4:40 AM said...

Great...I badly need this..
I have one last doubt are u pulling the whole table data from the database and paging them ?
or just pulling the data according to the paging which you wan to display for that page..???

Post a Comment