Search code examples
c#asp.netpaginationdatagrid

DataGrid won't show data of the next page


my DataGrid shows data from the first page no matter what page i clicked, i have searched for solution but nothing works. i have put BindGrid on !IsPostBack and rebind the grid on grid1_PageIndexChanging. here is my codes:

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

private int GetNumItems()
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = gc.GetWebConfigConnectionStringAIS();
    con.Open();
    string query = "SELECT COUNT(*) FROM dbo.TestingLatihan";
    SqlCommand cmd = new SqlCommand(query, con);
    Int32 totalRow = (Int32)cmd.ExecuteScalar();
    con.Close();

    return totalRow;
}

protected void grid1_PageIndexChanging(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
    grid1.CurrentPageIndex = e.NewPageIndex;
    BindGrid();
}

private void BindGrid() 
{
    grid1.VirtualItemCount = GetNumItems();
    grid1.PageIndexChanged += new DataGridPageChangedEventHandler(grid1_PageIndexChanging);

    SqlConnection con = new SqlConnection();
    con.ConnectionString = gc.GetWebConfigConnectionStringAIS();
    con.Open();
    string query = "SELECT * FROM dbo.TestingLatihan";
    SqlCommand cmd = new SqlCommand(query, con);

    SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    myAdapter.Fill(dt);

    grid1.DataSource = dt;
    grid1.DataBind();

    con.Close();  
}

and my .aspx

<asp:DataGrid ID="grid1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" Width="100%" 
OnItemCommand="dtgItemCommand" AllowPaging="True" PageSize="5" 
AllowCustomPaging="True" OnPageIndexChanged="grid1_PageIndexChanging" 
EnableViewState="true">
   <AlternatingItemStyle CssClass="tdgenap" />
   <ItemStyle CssClass="tdganjil" HorizontalAlign="Center" />
      <HeaderStyle HorizontalAlign="Center" Height="30px" CssClass="tdjudul"></HeaderStyle>
          <Columns>
             <asp:TemplateColumn HeaderText="ID" >
                <HeaderStyle Font-Underline="false" Height="15px" Width="5%" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
                        <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        <ItemTemplate>
                            <asp:Label id="lblID" runat="server" text='<%#DataBinder.Eval(Container.DataItem, "ID_")%>'></asp:Label>
                        </ItemTemplate>
            </asp:TemplateColumn>
            <PagerStyle Font-Bold="True" ForeColor="black" HorizontalAlign="Center" Wrap="True" Mode="NumericPages" />
             <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>

Solution

  • @vin, here it seems like you enable both property AllowPaging="True" & AllowCustomPaging="True". As default code for it is AllowPaging="True" then i try with that see my below example. Might be it help you to sort out your issue.

    your aspx.cs code should be similar as

     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        protected void grid1_PageIndexChanging(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
            grid1.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }
    
        private int GetNumItems()
        {
            int totalRow = 15;
            return totalRow;
        }
        private void BindGrid()
        {
    
            grid1.DataSource = GetTable();
            grid1.DataBind();
        }
        public DataTable GetTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Name", typeof(string));
            table.Rows.Add(1, "David");
            table.Rows.Add(2, "Sam");
            table.Rows.Add(3, "Christoff");
            table.Rows.Add(4, "Janet");
            table.Rows.Add(5, "Melanie");
            table.Rows.Add(6, "David1");
            table.Rows.Add(7, "Sam1");
            table.Rows.Add(8, "Christoff1");
            table.Rows.Add(9, "Jane1t");
            table.Rows.Add(10, "Melanie1");
            return table;
        }
    

    your .aspx code code should be similar as

            <asp:DataGrid ID="grid1" runat="server" AutoGenerateColumns="False"
                CellPadding="4" ForeColor="#333333" Width="100%"
               AllowPaging="True" PageSize="5" 
                OnPageIndexChanged="grid1_PageIndexChanging"
                >
                <AlternatingItemStyle CssClass="tdgenap" />
                <ItemStyle CssClass="tdganjil" HorizontalAlign="Center" />
                <HeaderStyle HorizontalAlign="Center" Height="30px" CssClass="tdjudul"></HeaderStyle>
                <Columns>
                    <asp:TemplateColumn HeaderText="ID">
                        <HeaderStyle Font-Underline="false" Height="15px" Width="5%" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
                        <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        <ItemTemplate>
                            <asp:Label ID="lblID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ID")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
                <PagerStyle Font-Bold="True" ForeColor="black" HorizontalAlign="Center" Wrap="True" Mode="NumericPages" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            </asp:DataGrid>
    

    Let me know is that solution works for your or not?