.ASPX page code
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging"
OnDataBound="GridView1_DataBound" AutoGenerateColumns="False" DataKeyNames="ProductID">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="ProductNumber" HeaderText="ProductNumber" SortExpression="ProductNumber" />
<asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" />
<asp:BoundField DataField="StandardCost" HeaderText="StandardCost" SortExpression="StandardCost" />
<asp:BoundField DataField="ListPrice" HeaderText="ListPrice" SortExpression="ListPrice" />
</Columns>
<PagerTemplate>
<table width="100%">
<tr>
<td style="text-align: right">
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
</div>
</form>
.CS page Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String ConnStr = ConfigurationManager.ConnectionStrings["myConnection"].ToString();
//the sql query with paging logics
//here table name is "Orders" change this name as your requirement.
//"CustomerID" is the column by which you can sort the records, change it as per your requirement
String SQL = @"select * FROM [AdventureWorksLT].[SalesLT].[Product]";
//fetching data from database suing SqlDataAdapter Fill method to bind the Gridview
SqlDataAdapter ad = new SqlDataAdapter(SQL, ConnStr);
DataSet ds = new DataSet();
ad.Fill(ds);
//bind the grid with the ist data table---------remember that this dataset consist of two data tables
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
SetPaging();
}
private void SetPaging()
{
GridViewRow row = GridView1.BottomPagerRow;
int alphaStart = 65;
for (int i = 1; i < GridView1.PageCount; i++)
{
LinkButton btn = new LinkButton();
btn.CommandName = "Page";
btn.CommandArgument = i.ToString();
if (i == GridView1.PageIndex + 1)
{
btn.BackColor = Color.BlanchedAlmond;
}
btn.Text = Convert.ToChar(alphaStart).ToString();
btn.ToolTip = "Page " + i.ToString();
alphaStart++;
PlaceHolder place = row.FindControl("PlaceHolder1") as PlaceHolder;
place.Controls.Add(btn);
Label lbl = new Label();
lbl.Text = " ";
place.Controls.Add(lbl);
}
}
}
No comments:
Post a Comment