Click here to Skip to main content
15,913,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem Im facing when i apply paging to gridview is that paging doesnt appear in gridview when i run program in browser. But in visual studio in design section paging appears. It also doesnt show any error when i run it in browser.

below is code

aspx code

ASP.NET
 <asp:GridView ID="GridView1" align="center"  OnRowDataBound="GridView1_RowDataBound"  runat="server" AllowPaging="True" 
        PageSize="4" onpageindexchanging="GridView1_PageIndexChanging">
    <HeaderStyle BackColor="ButtonShadow" ForeColor="White" />
    <rowstyle backcolor="LightCyan"  
           forecolor="DarkBlue"
           font-italic="true"/>

        <alternatingrowstyle backcolor="PaleTurquoise"  
          forecolor="DarkBlue"
          font-italic="true"/>
    <pagerstyle CssClass="gridview">

</pagerstyle>
    </asp:GridView>



.cs code

C#
protected void Page_Load(object sender, EventArgs e)
       {
           Getdata();



       }
       protected void Getdata()
       {
           con.Open();
           SqlCommand cmd = new SqlCommand("select  Title, InternshipStartDate, ApplicationDeadline,null as 'Applicants',null as 'Accepted',null as ' ', null as 'Action',null as '   ',InternshipID from Internship  where CompanyID=" + Session["CompanyID"], con);

           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           if (dt.Rows.Count > 0)
           {
               GridView1.DataSource = dt;
               GridView1.DataBind();





           }
           con.Close();

       }
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.Header)
           {
               e.Row.Cells[8].Visible = false;
               e.Row.Cells[3].Visible = false;
               e.Row.Cells[4].Visible = false;
           }
          // e.Row.Cells[5].Attributes.Add("onclick", "window.location='Internship_preview.aspx?InternshipID=" + e.Row.Cells[8].Text + "'");
           //e.Row.Cells[6].Attributes.Add("onclick", "window.location='Edit_Internship.aspx?InternshipID=" + e.Row.Cells[8].Text + "'");
           //e.Row.Cells[7].Attributes.Add("onclick", "window.location='Delete_Internship.aspx?InternshipID=" + e.Row.Cells[8].Text + "'");
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               e.Row.Cells[8].Visible = false;
               e.Row.Cells[3].Visible = false;
               e.Row.Cells[4].Visible = false;

               Button btn = new Button();
               //btn.Text = "preview";
               e.Row.Cells[5].Controls.Add(btn);
               btn.CausesValidation = false;
               btn.Text = "preview";
               btn.PostBackUrl = "Internship_preview.aspx?InternshipID=" + e.Row.Cells[8].Text + "";
               btn.CssClass = "button";
               //e.Row.Cells[5].Text = "Preview Internship";



               Button btn1 = new Button();
               //btn.Text = "preview";
               e.Row.Cells[6].Controls.Add(btn1);
               //btn1.CausesValidation = false;
               btn1.Text = "edit";
               btn1.PostBackUrl = "Edit_Internship.aspx?InternshipID=" + e.Row.Cells[8].Text + "";
               //e.Row.Cells[6].Text = "Edit Internship";
               btn1.CssClass = "button";

               Button btn2 = new Button();
               //btn.Text = "preview";
               e.Row.Cells[7].Controls.Add(btn2);

               btn2.CausesValidation = false;
               btn2.Text = "close";
               btn2.PostBackUrl = "Delete_Internship.aspx?InternshipID=" + e.Row.Cells[8].Text + "";
               //e.Row.Cells[7].Text = "Delete Internship";
               btn2.CssClass = "button";

               Label lb1 = new Label();
               e.Row.Cells[3].Controls.Add(lb1);

           }

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


            }
         }
Posted

1 solution

Paging will appear after
1. AllowPaging="True"
2.
C#
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                GridView1.PageIndex = e.NewPageIndex;
                Getdata();
            }


3. Paging data should be more than PageSize="4"

Hope so this much things necessary. Where your code is perfect
 
Share this answer
 
Comments
jayraj86 7-May-14 12:05pm    
Got it. Thanks a lot!
[no name] 7-May-14 12:10pm    
welcome:)
jayraj86 7-May-14 12:17pm    
:) Not able to solve this other problem - http://www.codeproject.com/Questions/768674/Paging-in-gridview-Specified-argument-was-out-of-t see if you can solve

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900