Explanation: 1) To give paging to datagrid, first we should set
AllowPaging="True" property. By default AllowPaging is false. So it does not allow to do paging unless we set AllowPaging="True".
2) Then we should set OnPageIndexChanged="datagrid_pageindexchanged". datagrid_pageindexchanged is a method on the server side which we should write.( Just like On OnItemCommand="dgrid_itemcommand" which we give for update and delete.)
That means whenever we click on a page number, datagrid_pageindexchanged method is called on server side.
3) We should set the PageSize. PageSize is the number of rows displayed in one page. If we set
PageSize="5" then we can see 5 rows in one page. (If we do not give PageSize property, then by default, it will take PageSize="10" and displayes 10 rows per page.)
4) Set PagerStyle-Mode property. We can set PagerStyle-Mode="NumericPages" or PagerStyle-Mode="NextPrev". If we give PagerStyle-Mode="NumericPages", then numbers will be displayed for paging just as we see in some sites like faqs in sap.
If PagerStyle-Mode="NextPrev", then we will just see Next and Previous links. Put one value at a time, run and see the difference. If we set PagerStyle-Mode="NextPrev" we can set more two properties also... they are
PagerStyle-NextPageText="Next" and PagerStyle-PrevPageText="Prev". Put these properties and run the project, you will see what they mean. You can give any value to PagerStyle-NextPageText. eg: N or Nex or Next etc. Similarly
for PagerStyle-PrevPageText eg: P or Prev or Previous.
In the server side write the following code
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=NIGHTWING\\SQL2005;Database=AEGIS;User ID=firstsportsuser;Password=firstsportsuser;");
SqlDataAdapter da = new SqlDataAdapter("select * from CMSFolder", con);
DataSet ds = new DataSet();
da.Fill(ds);
dgrid.DataSource = ds;
dgrid.DataBind();
}
protected void datagrid_pageindexchanged(object sender, DataGridPageChangedEventArgs e)
{
dgrid.CurrentPageIndex = e.NewPageIndex;
SqlConnection con = new SqlConnection("your connectionstring");
SqlDataAdapter da = new SqlDataAdapter("select * from tablename", con);
DataSet ds = new DataSet();
da.Fill(ds);
dgrid.DataSource = ds;
dgrid.DataBind();
}
}
In Page_Load method we will write normal code to bind data.
Next in datagrid_pageindexchanged method we will set the page index(page index means the page number that we click) first by this code:
dgrid.CurrentPageIndex = e.NewPageIndex;
In the above line, again the argument e gets with it NewPageIndex property which is the page number on which we click.
After setting the page number, we will as usual open Sqlconnection and bind data to dgrid.
None
0 Points
9 Posts
Code for paging
Aug 10, 2007 04:46 AM|mavis|LINK
Code for paging:
In the html write the following code
<asp:DataGrid ID="dgrid" runat="server" AllowPaging="true" OnPageIndexChanged="datagrid_pageindexchanged" PageSize="2" PagerStyle-Mode="NumericPages" PagerStyle-NextPageText="Next" PagerStyle-PrevPageText="Prev">
</asp:DataGrid>
Explanation: 1) To give paging to datagrid, first we should set AllowPaging="True" property. By default AllowPaging is false. So it does not allow to do paging unless we set AllowPaging="True".
2) Then we should set OnPageIndexChanged="datagrid_pageindexchanged". datagrid_pageindexchanged is a method on the server side which we should write.( Just like On OnItemCommand="dgrid_itemcommand" which we give for update and delete.)
That means whenever we click on a page number, datagrid_pageindexchanged method is called on server side.
3) We should set the PageSize. PageSize is the number of rows displayed in one page. If we set PageSize="5" then we can see 5 rows in one page. (If we do not give PageSize property, then by default, it will take PageSize="10" and displayes 10 rows per page.)
4) Set PagerStyle-Mode property. We can set PagerStyle-Mode="NumericPages" or PagerStyle-Mode="NextPrev". If we give PagerStyle-Mode="NumericPages", then numbers will be displayed for paging just as we see in some sites like faqs in sap. If PagerStyle-Mode="NextPrev", then we will just see Next and Previous links. Put one value at a time, run and see the difference. If we set PagerStyle-Mode="NextPrev" we can set more two properties also... they are PagerStyle-NextPageText="Next" and PagerStyle-PrevPageText="Prev". Put these properties and run the project, you will see what they mean. You can give any value to PagerStyle-NextPageText. eg: N or Nex or Next etc. Similarly for PagerStyle-PrevPageText eg: P or Prev or Previous.
In the server side write the following code
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=NIGHTWING\\SQL2005;Database=AEGIS;User ID=firstsportsuser;Password=firstsportsuser;");
SqlDataAdapter da = new SqlDataAdapter("select * from CMSFolder", con);
DataSet ds = new DataSet();
da.Fill(ds);
dgrid.DataSource = ds;
dgrid.DataBind();
}
protected void datagrid_pageindexchanged(object sender, DataGridPageChangedEventArgs e)
{
dgrid.CurrentPageIndex = e.NewPageIndex;
SqlConnection con = new SqlConnection("your connectionstring");
SqlDataAdapter da = new SqlDataAdapter("select * from tablename", con);
DataSet ds = new DataSet();
da.Fill(ds);
dgrid.DataSource = ds;
dgrid.DataBind();
}
}
In Page_Load method we will write normal code to bind data.
Next in datagrid_pageindexchanged method we will set the page index(page index means the page number that we click) first by this code:
dgrid.CurrentPageIndex = e.NewPageIndex;
In the above line, again the argument e gets with it NewPageIndex property which is the page number on which we click.
After setting the page number, we will as usual open Sqlconnection and bind data to dgrid.
Thats it...[:D]