在 asp.net 中启用分页功能来填充数据网格

发布于 2024-09-24 06:42:55 字数 221 浏览 0 评论 0原文

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

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

如果有人能告诉我如何实施它,我将不胜感激。

提前致谢,

格雷格

I have datagrid with paging enabled that can have 10 rows per page. Also I have DataTable with 16 rows. I want to fill the datagrid dynamically with 'for' loop to go over all the DataTable and fill the DataGrid.

I understand that there is a problem when the counter will hit row 11. Do I need to change the page of the datagrid when counter will be 11? Because it doesnt let me add more than 10 rows in the datagrid.

Would appriciate if someone can tell me how to implement it.

Thanks in advance,

Greg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无远思近则忧 2024-10-01 06:42:55

这几乎就是我会做的。我没有使用 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;
            }
        }
    }

这给出:

alt text
替代文字

This is pretty much how I'd do it. I'm not using a for as the conditional checking of checkboxes is in ItemDataBound, by doing it this way the DataGrid will do all the paging for me.

Markup:

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

Code-behind:

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

This gives:

alt text
alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文