asp gridview paging code

Posted by Gopinath under ASP.NET on 3/21/2012 | Points: 10 | Views : 5241 | Status : [Member] | Replies : 4
Hi,
Anybody tell me the code for asp gridview paging.it will useful to me.

C.Gopinath


Responses

Posted by: Hmanjarawala on: 3/21/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,
in aspx page


<asp:GridView ID="GridView1" runat="server" AllowPaging="true" OnPageIndexChanging="PaginateTheData" PageSize="2" PagerSettings-Mode="Numeric" EmptyDataText="<p><b>No records found.</b></p>" />


in code behind

string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindData();

}

}

/// <summary>

/// Paginates the data.

/// </summary>

/// <param name="sender">The sender.</param>

/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/>

protected void PaginateTheData(object sender, GridViewPageEventArgs e)

{

GridView1.PageIndex = e.NewPageIndex;

this.BindData();

}

/// <summary>

/// Binds the data.

/// </summary>

/// <returns></returns>

private void BindData()

{

DataTable table = new DataTable();

// get the connection

using (SqlConnection conn = new SqlConnection(_connStr))

{

// write the sql statement to execute

string sql = "SELECT AutoId, FirstName, LastName, Age, Active FROM PersonalDetail ORDER By AutoId";

// instantiate the command object to fire

using (SqlCommand cmd = new SqlCommand(sql, conn))

{

// get the adapter object and attach the command object to it

using (SqlDataAdapter ad = new SqlDataAdapter(cmd))

{

// fire Fill method to fetch the data and fill into DataTable

ad.Fill(table);

}

}

}

GridView1.DataSource = table;

GridView1.DataBind();

}



Mark this as answer, if it helps you..............

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Gopinath, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajni.Shekhar on: 3/22/2012 [Member] Bronze | Points: 25

Up
0
Down
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
this.gv.PageIndex = e.NewPageIndex;
this.gv.DataSource = this.ds; //dataset globally declared and fetched data in ds
this.gv.DataBind();
}
catch (Exception ex)
{
}
}

Thanks,
Rajni Shekhar

Gopinath, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/22/2012 [Member] Silver | Points: 25

Up
0
Down
Hi ,
Try this.
//aspx page in gridview
<asp:GridView ID="gv" runat="server" AllowPaging="true" OnPageIndexChanging="gv_PageIndexChangin" PageSize="5">
</GridView>
//Page_Load
//call getViewData() which return a dataset
//bind dataset to gridview


protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
this.gv.PageIndex = e.NewPageIndex;
this.gv.DataSource =getViewData();
this.gv.DataBind();
}
catch (Exception ex)
{
}
}

Thanks,
Sanjay

Gopinath, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 3/22/2012 [Member] Starter | Points: 25

Up
0
Down
Hi ,

add gridview in your aspx page
then set Allow paging=true


<asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="10" OnPageIndexChanging="GridView1_PageIndexChanging">
</asp:GridView>


on page load Bind gridview

protected void Page_Load(object sender, EventArgs e)

{
if (!IsPostBack)
{
// code to bind gridview
}
}


then on PageIndexChanging event of gridview write code as follow

  protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

{
GridView1.PageIndex = e.NewPageIndex;
// code to bind gridview

}


thanks


R D Patel

Gopinath, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response