Search code examples
c#asp.netgridviewpagination

GridView PageSize does not display records on Next Page


when I use AllowPaging="True" property in gridview, and navigate to Page 2, my code throws me out at the home-page of my website. Code is not showing records list on page 2 instead. Neither it is throwing any errors so I cannot determine where I am going wrong. The .aspx page code ges like this is:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="10" CellSpacing="4" OnPageIndexChanging="GridView1_PageIndexChanging" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="DocumentsId" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" ForeColor="#333333" OnRowCancelingEdit="GridView1_RowCancelingEdit" >
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

.cs file code is:

protected void Page_Load(object sender, EventArgs e)
{
    BindGrid();
    MultiView1.SetActiveView(vHome);
    btnBacktoHome.Visible = false;
    lblStatus.Visible = false;       
}

public void BindGrid()
{
    SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS; Initial Catalog=PIMS; Integrated Security=true;");
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            String sql = "select [DocumentsID],[Ref],[Subject],[Src],[Dst],[Medium],[Date_Printed],[Date_Received],[Document_Type],[Action_Required],[Due_Date],[Actual_Date],[Content],[Tag],[Issue_No],[Attachment],[Notes],[Assigned_To],[Reply_Ref],[Priority],[Status],[Response],[Physical_File_No],[Physical_Rack_Location] from dbo.Documents";
            cmd.Connection = con;
            cmd.CommandText = sql;
            con.Open();
            DataSet ds = new DataSet();
            using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
            {
                adp.Fill(ds);

            } GridView1.DataSource = ds.Tables[0]; 
            GridView1.DataBind();
        }

        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
    }
}

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

Any help will be much appreciated. Thanks in advance.


Solution

  • Do you also call BindGrid from Page_Load without if(!IsPostBack)-check?

    Yes without if(!IsPostBack)

    Then add that :)

    protected void Page_Load(object sender, EventArgs e) 
    { 
        if(!IsPostBack)
        {
            BindGrid(); 
            MultiView1.SetActiveView(vHome); b
            btnBacktoHome.Visible = false; 
            lblStatus.Visible = false; 
        }
    }
    

    Otherwise you will databind the GridView with the old values from database which also prevents all events from being triggered.