Powered By Blogger

Search

Thursday, April 17, 2008

ASP.NET 2.0 GridView control in Paging and Sorting

ASP.NET 2.0 GridView control in Paging and Sorting
This article will describe how you can manually implement paging and sorting for the new GridView control in ASP.NET 2.0

First of all I set the AlowPaging and AllowSorting properties to true, then specified page PageSize attribute of GridView to 10. For each bound Column (that I want to be sorted) I specified the SortExpression attribute same as the column name in the database table.

After doing these initial changes you need to handle the Sorting event. There are two events for sorting one is Sorting and the other is Sorted. Sorted is called just after the GridView is sorted and it’s the Sorting event in which you will need to handle the sorting logic.

Here a GridViewSortEventArgs is provided from which you can get the column’s SortExpression and SortDirection. You will need to re fetch the data from database and bind that to the GridView and you need to restructure the query with order by and DESC or ASC clauses.

For Paging there is the event PageIndexChanging that gives the index of the new page clicked. Here you just need to set the PageIndex property of the GridView to the new Index and re bind the data to GridView.

Design Page :



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewTest.aspx.cs" Inherits="GridViewTest" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Grid View Sorting</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" OnPageIndexChanging="GridView1_PageIndexChanging"
OnSorting="GridView1_Sorting" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ProjectID" HeaderText="ProjectID" SortExpression="ProjectID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="ShortName" HeaderText="ShortName" SortExpression="ShortName" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="EstimatedStartDate" HeaderText="EstimatedStartDate" SortExpression="EstimatedStartDate"/>
</Columns>

</asp:GridView>
<asp:HiddenField ID="Direction" runat="server" />
<asp:HiddenField ID="Expression" runat="server" />
</div>
</form>
</body>
</html>



Code Page :

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GridViewTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = DataBinder().Tables[0];
GridView1.DataBind();
Direction.Value= "ASC";
}
}
public DataSet DataBinder()
{
SqlConnection con = new SqlConnection("Data Source=Test;Initial Catalog=TESTDB_Dev;User ID=User;Password=Pass");
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("Select ProjectID, Name ,ShortName ,Description, StartDate from Project", con);
DataSet ds = new DataSet();
adp.Fill(ds);
return ds;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
Expression.Value= e.SortExpression;
if (Direction.Value!=("ASC"))
{
Direction.Value= "ASC";
}
else
{
Direction.Value= "DESC";
}
Sort(Expression.Value+" "+ Direction.Value);
}

public void Sort(string Exp)
{
DataView DV = new DataView(DataBinder().Tables[0]);
try
{
DV.Sort = Exp;
}
catch (Exception ex)
{

}
GridView1.DataSource = DV;
GridView1.DataBind();
}


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
Sort(Expression.Value+ " " + Direction.Value);
}
}



No comments: