DataGrid 不会显示下一页的数据

问题描述 投票:0回答:1

我的 DataGrid 显示第一页的数据,无论我单击哪个页面,我都在寻找解决方案,但没有任何效果。我已将

BindGrid
放在
!IsPostBack
上,并将网格重新绑定到
grid1_PageIndexChanging
上。这是我的代码:

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();  
}

和我的.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>
c# asp.net pagination datagrid
1个回答
1
投票

@vin,这里似乎您启用了两个属性AllowPaging =“True”和AllowCustomPaging =“True”。作为它的默认代码是AllowPaging =“True”,然后我尝试使用它,请参阅下面的示例。也许它可以帮助您解决问题。

您的 aspx.cs 代码应类似于

 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;
    }

您的 .aspx 代码应该类似于

        <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>

让我知道该解决方案是否适合您?

© www.soinside.com 2019 - 2024. All rights reserved.