Sunday, 4 January 2015

GridView Paging Using PageIndexChanging Event in Asp.Net C#

GridView Paging Using PageIndexChanging Event

GridView Paging Used to Move Next Page  Using PageIndexChanging Event in Asp.Net C#.
    
                            Download Coding

                                                Download

                                     DEMO

HTML CODING

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <table><tr></td><td>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="3" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging">
             <Columns>
                 <asp:BoundField DataField="id" HeaderText="ID" />
                 <asp:BoundField DataField="city" HeaderText="City" />
                 <asp:BoundField DataField="country" HeaderText="Country" />
             </Columns>
             <HeaderStyle BackColor="Red" Height="50px" />
             <RowStyle BackColor="#FFCC66" Height="70px" Width="120px" />
         </asp:GridView>
         </td>
         </tr></table>
        </div>
        </form>
</body>
</html>



C# CODING




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.Data.SqlClient;

public partial class GridPaging : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    SqlDataReader rd;
    DataSet ds;
    string query;

    public void dbcon()
    {
        string connn = (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con = new SqlConnection(connn);
        con.Open();

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            bind1();

        }
    }
    protected void bind1()
    {
        dbcon();
        query = "select * from grid";
        cmd = new SqlCommand(query, con);
        adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        adp.Fill(ds);
        rd = cmd.ExecuteReader();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();

        }


        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            // GridView1.FooterRow.Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
            GridView1.Rows[0].Cells[0].Text = "No Records Found";
        }

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bind1();
    }
}






























































0 comments:

Post a Comment