【问题标题】:Adding data to 3 gridview on button click from 1 datatable从 1 个数据表单击按钮时将数据添加到 3 个网格视图
【发布时间】:2014-11-07 03:42:07
【问题描述】:

我有一个包含 1 - 12 数字的数据表

在按钮单击时,我想将这 12 个数字分配给我的 3 个网格视图,

GridView1 应该包含 1 4 7
GridView2 应该包含 2 5 8
GridView3 应该包含 3 6 9

数字将像这样分配给 GridViews

这是我的代码:

 DataTable dt = new DataTable();
    DataColumn dc = new DataColumn();
    if (dt.Columns.Count == 0)
    {
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Grade", typeof(string));
    }

    for (int i = 0; i <= 2; i++)
    {
        GridView A = new GridView { ID = "gv" + i.ToString() };
        Div1.Controls.Add(A);
    }

    ctr = 0;
    for (int x = 0; x <= count_table - 1; x++)
    {
        if (ctr == 3)
        { ctr = 0; }
        GridView B = Div1.FindControl("gv" + ctr.ToString()) as GridView;
        DataRow NewRow = dt.NewRow();
        NewRow[0] = stud_name[x];
        NewRow[1] = grade[x];
        dt.Rows.Add(NewRow);
        B.DataSource = dt;
        B.DataBind();
        ctr++;
    }

【问题讨论】:

  • 到目前为止你做了什么?

标签: asp.net gridview datatable


【解决方案1】:

您可以使用DataTable.Select 方法或LINQ 示例如下:

DataTable selectedTable = tb.AsEnumerable()
                            .Where(yourcondition)
                            .CopyToDataTable();

这将根据过滤值创建一个新的DataTable

如果你使用DataTable.Select

string expression = "Modul =" + value;
DataRow[] selectedRows = tb.Select(expression);

然后使用新的数据表绑定你的gridview。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-14
    • 2014-06-02
    • 1970-01-01
    • 2016-12-14
    • 2020-07-18
    • 2010-12-15
    • 1970-01-01
    相关资源
    最近更新 更多