【问题标题】:DataGrid's Paging is not workingDataGrid 的分页不起作用
【发布时间】:2018-10-27 09:23:22
【问题描述】:

我正在尝试向我的数据网格添加分页,但它没有显示,而是数据网格仅显示第 1 页,我无法单击页码

这是我的 aspx

<asp:DataGrid ID="grid1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" PageSize="5" AllowCustomPaging="true" 
OnPageIndexChanged="grid1_PageIndexChanging">

<HeaderStyle HorizontalAlign="Center" Height="30px"></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="left" Wrap="True" Mode="NumericPages" />
      <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
      </asp:DataGrid>

在我的 aspx.cs 上我添加了BindGrid(),但它不起作用

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

protected void grid1_PageIndexChanging(object source, DataGridPageChangedEventArgs e)
{
    grid1.CurrentPageIndex = e.NewPageIndex;
    grid1.VirtualItemCount = 1000;
    BindGrid();
}

private void BindGrid() 
{
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    con.ConnectionString = gc.GetWebConfigConnectionStringAIS();
    con.Open();
    string query = "SELECT ID_ FROM dbo.Testing";
    cmd = new SqlCommand(query, con);
    cmd.CommandType = CommandType.Text;

    using (SqlDataReader dr = cmd.ExecuteReader())
    {
        grid1.DataSource = dr;
        grid1.DataBind();
    }

    con.Close();
    grid1.Visible = true;
}

【问题讨论】:

  • 删除AllowCustomPaging="true"
  • @VDWWD 如果我删除它会发生错误。它说AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID 'grid1' when AllowPaging is set to true and the selected data source does not implement ICollection.
  • 啊。我在测试时也删除了 VirtualCount。但是为什么不使用 GridView 呢? DataGrid 已经被它取代了很长时间了。

标签: c# asp.net datagrid pagination


【解决方案1】:

验证您是否有足够的数据可以填充到一页以上

【讨论】:

  • 我做到了,当我在数据库中有 7 条记录时,我将页面大小设置为 5
【解决方案2】:

使用 DataSet 解决您的问题

private void BindGrid() 
{
    //declare DataSet attribute
    SqlDataAdapter da;  
    DataSet ds = new DataSet(); 

    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    con.ConnectionString = gc.GetWebConfigConnectionStringAIS();
    con.Open();
    string query = "SELECT ID_ FROM dbo.Testing";
    cmd = new SqlCommand(query, con);
    cmd.CommandType = CommandType.Text;

    //here happens the magic
    cmd.Connection = con;  
    da = new SqlDataAdapter(cmd);  
    da.Fill(ds);  
    cmd.ExecuteNonQuery();

    //using dataset instead of reader will resolve your issue
    Grid.DataSource = ds;  
    Grid.DataBind();  

    using (SqlDataReader dr = cmd.ExecuteReader())
    {
        grid1.DataSource = dr;
        grid1.DataBind();
    }

    con.Close();
    grid1.Visible = true;
}

"DataGrid.AllowCustomPaging Property" 官方文档

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 2012-05-29
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多