ASP.NET - allow paging in gridview

Asked By saran vasan
10-Feb-12 02:31 AM
how to use allowpaging property in gridview in asp.net with c sharp?
  Sreekumar P replied to saran vasan
10-Feb-12 03:21 AM
Hi,

Gridview with multiple number rows at that time I tried to set paging for my gridview for that I have search gridview properties at that time I found different type of paging mode styles are available in gridview for paging those are

1.    1) Numeric
2.    2) NextPrevious
3.    3) NextPreviousFirstLast
4.    4) NumericFirstLast

To use these options in our gridview we need to set AllowPaging="true" and PageSize="5" Properties here pageSize you can change based on your requirement after set these properties we can use different type of pagerstting modes to show different type of paging for gridview.


Here is a example

<%@ Page language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
  <head runat="server">
  <title>GridView AllowPaging Example</title>
</head>
<body>
  <form id="form1" runat="server">
 
    <h3>GridView AllowPaging Example</h3>
 
    <asp:gridview id="CustomersGridView"
    datasourceid="CustomersSource"
    autogeneratecolumns="true"
    emptydatatext="No data available."
    allowpaging="true"
    runat="server">
 
    <pagersettings mode="Numeric"
      position="Bottom"      
      pagebuttoncount="10"/>
 
    <pagerstyle backcolor="LightBlue"
      height="30px"
      verticalalign="Bottom"
      horizontalalign="Center"/>
 
    </asp:gridview>
 
    <!-- This example uses Microsoft SQL Server and connects  -->
    <!-- to the Northwind sample database. Use an ASP.NET   -->
    <!-- expression to retrieve the connection string value   -->
    <!-- from the Web.config file.                -->
    <asp:sqldatasource id="CustomersSource"
    selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
    connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
    runat="server"/>
 
  </form>
  </body>
</html>
  kalpana aparnathi replied to saran vasan
10-Feb-12 03:44 AM
hi,

For allowing paging in gridview do this.

    set the gridview property AllowPaging=”True” and PageSize=”2″>

For more detail about it http://howtouseasp.net/how-to-use-gridview-control-with-database-and-paging-in-gridview-asp-net-c/
  Somesh Yadav replied to kalpana aparnathi
10-Feb-12 04:05 AM
Hi,
Gridview.Allowpaging Property,
Gets or sets a value indicating whether the paging feature is enabled.

Namespace:  http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.aspx
Assembly:  System.Web (in System.Web.dll)

In c#:-
public virtual bool AllowPaging { get; set; }

<asp:GridView AllowPaging="True|False" />

Property Value

Type: http://msdn.microsoft.com/en-us/library/system.boolean.aspx
true if the paging feature is enabled; otherwise, false. The default is false.

The following example demonstrates how to use the AllowPaging property to declaratively enable the paging feature in the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx control.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView AllowPaging Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView AllowPaging Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowpaging="true" 
        runat="server">

        <pagersettings mode="Numeric"
          position="Bottom"           
          pagebuttoncount="10"/>

        <pagerstyle backcolor="LightBlue"
          height="30px"
          verticalalign="Bottom"
          horizontalalign="Center"/>

      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>

    </form>
  </body>
</html>
  dipa ahuja replied to saran vasan
10-Feb-12 04:09 AM
Here is the simple example of gridview paging:
 
Set the AllowPaging = True for gridview and write this code in codebehind
 
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
     bindGrid();
   }
}
void bindGrid()
{
   SqlDataAdapter da = new SqlDataAdapter("select * from TableName", "Connection String");
   DataTable dt = new DataTable();   
  da.Fill(dt);
   GridView1.DataSource = dt;
   GridView1.DataBind();
}
 
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   GridView1.PageIndex = e.NewPageIndex;
   BindGrid();
   //Bind Grid
}
 
 
Create New Account
help
error as : Unable to cast object of type 'System.Data.DataSet' to type 'System.Data.DataTable'. and the snippet is : Inline Code : <%@ Page Language = "C#" AutoEventWireup = "true" CodeFile = "Gridview.aspx.cs" Inherits = "Gridview" %> <!DOCTYPE html PUBLIC "- / / W3C / / DTD transitional.dtd"> <html xmlns = "http: / / www.w3.org / 1999 / xhtml"> <head runat = "server"> <title> Untitled Page< / title> < / head> <body> <form id = "form1" runat = "server"> <div> <asp:GridView ID = "GV1" runat = "server using System.Xml.Linq; using System.Data.SqlClient; public partial class Gridview : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); Response.Write("Before Button Click"); } } public void BindData() { SqlConnection connect = new string strSql; strSql = "Select name, countryid, date from masternitin"; SqlCommand cmd = new SqlCommand(strSql, connect); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); GV1.DataSource = ds; Session["Finalview"] = ds; GV1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { Response.Write("After button Click"); } protected void GV1_RowEditing(object sender, GridViewEditEventArgs e) { GV1.EditIndex
HasRows) { DropDownList1.SelectedIndex = Convert .ToInt16(dr[1].ToString()); txthotelname.Text = dr[3].ToString(); / / adap = new SqlDataAdapter("select *from tbl_city where stateid = '" + DropDownList1.SelectedItem.Value + "'", con1); / / ds = new DataSet(); / / adap.Fill(ds HasRows) { DropDownList1.SelectedIndex = Convert .ToInt16(dr[1].ToString()); txthotelname.Text = dr[3].ToString(); adap = new SqlDataAdapter ( "select *from tbl_city where stateid = '" + DropDownList1.SelectedItem.Value + "'" , con1); ds = new DataSet (); adap.Fill(ds me how u binding ur dropdown list and what data ur dropdown have. . my full page codings: using System; using System.Configuration; using System.Data; using System.Linq; using System.Web Linq; using System.Data.SqlClient; using System.IO; public partial class _Default : System.Web.UI. Page { SqlConnection con = new SqlConnection ( ConfigurationManager .ConnectionStrings[ "newhotel" ].ConnectionString); SqlConnection con1 = new SqlConnection ( ConfigurationManager .ConnectionStrings[ "newhotel" ].ConnectionString); SqlCommand cmd; SqlDataAdapter adap; DataSet ds; SqlDataReader dr; Label dropdown = new Label (); Label dopdown = new Label (); string g int id; protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { Label1.Text = Session[ "username" ].ToString(); / / Label2.Text = Session["id"].ToString(); bindgrid(); dropbind(); DropDownList2.Items.Clear(); DropDownList2.Items.Add( "Select" ); } } protected void Button1_Click( object sender, EventArgs e) { try { if (con.State = = ConnectionState .Closed) { con.Open(); } if (txthotelname.Text ! = "" ) { cmd = new SqlCommand ex.ToString()); } finally { if (con.State = = ConnectionState .Open) { con.Close(); } } } protected void Button2_Click( object sender, EventArgs e) { DropDownList1.Items.Clear(); DropDownList1.Items.Add( "Select" ); dropbind(); DropDownList2.Items.Clear(); DropDownList2.Items.Add
be a pager 'pervious / next with numeric' this is what i did [B]in aspx page[ / B] <asp:GridView ID = "TableGridView" OnPageIndexChanging = "TableGridView_PageIndexChanging" runat = "server" AutoGenerateColumns = "False" AllowPaging = "True" AllowSorting = "True left: 260px; position: absolute; top: 143px" Text = "Connect & Populate" OnClick = "btnConnect_Click" / > [B]in code behind page [ / B] public partial class _Default : System.Web.UI.Page { public static DataTable Table = new DataTable(); ArrayList ParameterArray = new ArrayList(); protected void Page_Load(object sender, EventArgs e) { if (IsPostBack && (bool)Session["IsConnectionInfoSet"] = = true) CreateTemplatedGridView(); } protected void TableGridView_PageIndexChanging(object sender GridViewPageEventArgs e) { / / CreateTemplatedGridView(); TableGridView.PageIndex = e.NewPageIndex; TableGridView.DataBind(); } protected void btnConnect_Click(object sender, EventArgs e) { Session["IsConnectionInfoSet"] = true; CreateTemplatedGridView(); } void PopulateDataTable() { Table = new DataTable(); TableGridView.Columns.Clear(); SqlDataAdapter adapter
button . . . the select rows should bind in the another grid view control in my main page how can i do this thanks in advance Muthu hi, here is a good link conString"].ConnectionString; string query = " select CustomerID, ContactName, City from customers"; SqlConnection con = new SqlConnection(constr); SqlDataAdapter sda = new SqlDataAdapter(query, con); DataTable dt = new DataTable(); sda.Fill(dt); gvAll.DataSource = dt; gvAll.DataBind(); } Maintaining the state of Checkboxes while paging example since its job is to keep track of the selected rows irrespective of which page it belongs and also maintain the state of the checkboxes while pagination so that user selections are not lost when he navigates from one page to another. To facilitate this I have made use of the following two functions 1 retrieves the records for which the user has checked the checkbox, adds them to a DataTable and then saves the DataTable to ViewState C# private void GetData() { DataTable dt; if (ViewState
gridView I am Using Gridview in my application i have 1 aspx page for employee list in this page three dropdownlist 1 for Sex(Male, Female) 2nd for Marital Status (single, Married) and 3rd for city(. . . . . . .) when the Page is first time load then it display all the record in grid view if user click in next page then it display next 20 record in page when user select on male from Sex dropdownlist then it display first 20 record of next then it should be display next 20 record in gridview My problem is that page does not display next 20 male members what can i do for this problem please things- 1. Write following code in PageIndexChanging() event of GridView- protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridView1.DataSource = ShowData(); GridView1.DataBind(); } 2. set AllowPaging Property of Hello, Your problem is in the Page_Load event. . you are binding your Dropdownlist on every page load Put your page_load code inside following If condition will work perfectly if (!isPostback) { / / Your