【发布时间】: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