【问题标题】:Fill datagrid with paging enabled in asp.net在 asp.net 中启用分页填充数据网格
【发布时间】:2011-04-15 02:46:54
【问题描述】:

我有启用分页的数据网格,每页可以有 10 行。我也有 16 行的 DataTable。我想用“for”循环动态填充数据网格,以遍历所有数据表并填充数据网格。

我了解当计数器到达第 11 行时会出现问题。当计数器为 11 时,我是否需要更改数据网格的页面?因为它不允许我在数据网格中添加超过 10 行。

如果有人可以告诉我如何实现它,将不胜感激。

提前致谢,

格雷格

【问题讨论】:

  • 您是否有理由使用 for 循环手动填充 DataGrid?通常你只需设置数据源,然后调用DataBind
  • 是的。因为我在那个数据网格中有模板复选框,在某些情况下我需要检查它们,而在某些情况下不需要。
  • 你不能在 ItemDataBound 中这样做吗?

标签: asp.net datagrid datatable paging


【解决方案1】:

这几乎就是我的做法。我没有使用for,因为复选框的条件检查在 ItemDataBound 中,通过这种方式 DataGrid 将为我完成所有分页。

标记:

<asp:DataGrid runat="server" ID="MyDataGrid" AllowPaging="true" PageSize="10" OnPageIndexChanged="MyDataGrid_PageIndexChanged" OnItemDataBound="MyDataGrid_ItemDataBound" Autogeneratecolumns="false">
    <Columns>
        <asp:BoundColumn DataField="Number" HeaderText="Number" />
        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="CheckBox" />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>

代码隐藏:

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable numberDataTable;

        if (!IsPostBack)
        {
            // Build a 16-row DataTable
            numberDataTable = new DataTable();
            numberDataTable.Columns.Add(new DataColumn("Number"));

            for (int c = 1; c < 17; c++)
            {
                DataRow numberDataRow = numberDataTable.NewRow();
                numberDataRow[0] = c;
                numberDataTable.Rows.Add(numberDataRow);
            }

            ViewState.Add("Data", numberDataTable);

            // DataBind the table into the DataGrid
            MyDataGrid.DataSource = numberDataTable;
            MyDataGrid.DataBind();
        }
    }

    protected void MyDataGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
    {
        DataTable numberDataTable;

        // Get the DataTable out of Viewstate
        numberDataTable = (DataTable)ViewState["Data"];

        // Set the new page number
        MyDataGrid.CurrentPageIndex = e.NewPageIndex;

        // Bind the grid
        MyDataGrid.DataSource = numberDataTable;
        MyDataGrid.DataBind();
    }

    protected void MyDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        DataRow numberDataRow;

        // Selective checking of the CheckBox

        // Only do this for Item and ALternatingItem, we don't do this for headers, footers etc
        if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
        {
            numberDataRow = ((DataRowView)e.Item.DataItem).Row;

            // Check if we have an even number
            if ((int.Parse(numberDataRow[0].ToString()) % 2) == 0)
            {
                // Find our checkbox control in the DataGrid for the current row and check it
                CheckBox checkBox = (CheckBox)e.Item.FindControl("CheckBox");
                checkBox.Checked = true;
            }
        }
    }

这给出了:


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多