In .aspx file :
<asp:GridView ID="GridView1" runat="server" PageSize="5" AllowPaging="true" onpageindexchanging="GridView1_PageIndexChanging"><PagerSettings Mode="Numeric" />
</asp:GridView>
In .aspx.cs file :
public partial class ADO_DATACONTROLS_Paging : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//sspi=Security Support Provider Interface ~= trusted connection
if(!IsPostBack)
DisplayEmployees();
}
private void DisplayEmployees()
{
string strcn = "data source=ADMIN-PC\\SQLEXPRESS;initial catalog=db17;integrated security=sspi";
SqlConnection cn = new SqlConnection(strcn);
SqlCommand cmd = new SqlCommand("select * from employees", cn);
try
{
cn.Open();
DataSet ds = new DataSet();
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DA.Fill(ds);
Session["ds"] = ds;
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (SqlException ex)
{
}
finally
{
cn.Close();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
DataSet ds = (DataSet)Session["ds"];
DataTable dt = ds.Tables[0];
GridView1.DataSource = dt;
GridView1.PageIndex = e.NewPageIndex;//this code must be written between Datasource and DataBind
GridView1.DataBind();
}
}
Table structure:
eid | fname | lname | age | salary | dept | doj |
---|---|---|---|---|---|---|
1 | rajeev | sukla | 23 | 12000 | .net | 23-Oct-11 12:00:00 AM |
2 | sowmya | kumari | 23 | 19000 | db | 13-Nov-10 12:00:00 AM |
3 | kishore | kumar | 27 | 36000 | android | 16-Oct-11 12:00:00 AM |
4 | abimanyu | biswal | 22 | null | android | 20-Feb-10 12:00:00 AM |
5 | soni | kumar | 24 | 21800 | .net | 21-Jun-09 12:00:00 AM |
6 | anu | _singh | 22 | 12000 | db | 23-Oct-10 12:00:00 AM |
7 | _dinesh | moh%anty | 23 | 15000 | .net | 26-Aug-09 12:00:00 AM |
8 | nishala | _kumari | 22 | 18000 | db | 19-Jul-08 12:00:00 AM |
1 | rajeev | sukla | 23 | 12000 | .net | 23-Oct-11 12:00:00 AM |