Cybercrux

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”
OnPageIndexChanging=”gvProduct_PageIndexChanging”
OnRowDataBound=”gvProduct_RowDataBound”
OnSorting=”gvProduct_Sorting”>
<RowStyle BackColor=”#E3EAEB” VerticalAlign=”Middle” />
<HeaderStyle BackColor=”#1C5E55″ HorizontalAlign=”Left” Font-Bold=”True” ForeColor=”White” />
<AlternatingRowStyle BackColor=”White” />

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

<EmptyDataTemplate>
Sorry! Employee record was not found.
</EmptyDataTemplate>

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

 

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”);
try
{
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.DataBind();
}
else
{
gvProduct.DataSource = null;
gvProduct.DataBind();
}
}
catch (Exception oException)
{
// The connection failed. Display an error message.
}
}

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

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)
{
BindData();
}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<PagerTemplate>
<table border=”0″ style=”width: 100%;”>
<tbody>
<tr>
<td style=”float: right;”>
<asp:LinkButton CommandName=”Page” CommandArgument=”Prev” ID=”LinkButton2″ runat
&nbsp;<asp:LinkButton CommandName=”Page” CommandArgument=”Next” ID=”LinkButton3″
</td>
<td style=”clear: both”></td>
</tr>
</tbody>
</table>
</PagerTemplate>
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”
position=”Bottom”/>
<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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s