Everything is achievable through technology

Paging and sorting

<asp:GridView CellSpacing=”1″ ID=”gvProduct” PageSize=”25″ Width=”100%” runat=”server”
AutoGenerateColumns=”False” GridLines=”None” AllowPaging=”True” AllowSorting=”True”
<RowStyle BackColor=”#E3EAEB” VerticalAlign=”Middle” />
<HeaderStyle BackColor=”#1C5E55″ HorizontalAlign=”Left” Font-Bold=”True” ForeColor=”White” />
<AlternatingRowStyle BackColor=”White” />

<asp:BoundField HeaderText=”Sr.No.”>
<HeaderStyle HorizontalAlign=”Center” />
<ItemStyle HorizontalAlign=”Center” Width=”10%” />
<asp:BoundField HeaderText=”ProductID” SortExpression=”ProductID” DataField=”ProductID” />
<asp:BoundField HeaderText=”ProductName” SortExpression=”ProductName”
DataField=”ProductName” />
<asp:BoundField HeaderText=”UnitPrice” SortExpression=”UnitPrice” DataField=”UnitPrice” />

Sorry! Employee record was not found.

<EmptyDataRowStyle HorizontalAlign=”Center” Font-Size=”Larger” Font-Bold=”true” />
<PagerSettings Mode=”NextPrevious” Position=”TopAndBottom” NextPageText=”Next”
FirstPageText=”First” LastPageText=”Last” />
<asp:Button ID=”Button1″ runat=”server” Text=”First” CommandName=”Page”
Enabled=”<%# gvProduct.PageIndex > 0 %>” />
<asp:Button ID=”Button2″ runat=”server” Text=”Prev” CommandName=”Page”
Enabled=”<%# gvProduct.PageIndex > 0 %>” />
<span id=”Span1″ runat=”server”>Page
<%= gvProduct.PageIndex + 1%>of
<%= gvProduct.PageCount%>
<asp:Button ID=”Button3″ runat=”server” Text=”Next” CommandName=”Page”
Enabled=”<%# gvProduct.PageIndex + 1 < gvProduct.PageCount %>” />
<asp:Button ID=”Button4″ runat=”server” Text=”Last” CommandName=”Page”
Enabled=”<%# gvProduct.PageIndex + 1 < gvProduct.PageCount %>” />


private void BindData()
string connectionString = WebConfigurationManager.ConnectionStrings[“Northwind”].ConnectionString;
string selectSQL = “SELECT ProductID, ProductName, UnitPrice FROM Products”;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, “Products”);
if (ds.Tables[0].Rows.Count > 0)
DataView DV = ds.Tables[0].DefaultView;
if (Session[“sortBy”] != null)
DV.Sort = Session[“sortBy”].ToString();
gvProduct.DataSource = DV;
gvProduct.DataSource = null;
catch (Exception oException)
// The connection failed. Display an error message.

Sorting Event
protected void gvProduct_Sorting(object sender, GridViewSortEventArgs e)
Session[“sortBy”] = e.SortExpression;
Paging Event
protected void gvProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
gvProduct.PageIndex = e.NewPageIndex;

Page Load
On page load we need to call Bind Data functions
protected void Page_Load(object sender, EventArgs e)
Session[“sortBy”] = null;
if (!IsPostBack)















<table border=”0″ style=”width: 100%;”>
<td style=”float: right;”>
<asp:LinkButton CommandName=”Page” CommandArgument=”Prev” ID=”LinkButton2″ runat
&nbsp;<asp:LinkButton CommandName=”Page” CommandArgument=”Next” ID=”LinkButton3″
<td style=”clear: both”></td>
protected void GridView_RowCreated(Object sender, GridViewRowEventArgs e)
if(e.Row.RowType == DataControlRowType.Pager)
var prev = (LinkButton)e.Row.FindControl(“LinkButton2”);
prev.Visible = Grid.PageIndex > 0;
var next = (LinkButton)e.Row.FindControl(“LinkButton3″);
next.Visible = Grid.PageIndex < grid.PageCount – 1;

<pagersettings mode=”NextPrevious”
nextpagetext=”Next >>”
previouspagetext=”<< Previos”
<PagerStyle CssClass=”myPager” />

4 thoughts on “Paging and sorting

  1. nice information, many thanks to the author. i agree with your thought. thank you for your sharing. great post! it’s very useful for me

  2. Just wanna input on few general things, The website style and design is perfect, the content material is very good : D.

  3. thanks for the nice post.

  4. Pretty great post. I simply stumbled upon your blog and wanted to say that I have really enjoyed surfing around your blog posts. After all I will be subscribing for your rss feed and I hope you write once more soon!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s