C# 数据表中的每一行为每一列添加一个 asptablerow 和 asptablecell

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16282671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:37:56  来源:igfitidea点击:

foreach row in datatable add a asptablerow and asptablecell for each column

c#asp.netdatatable

提问by AlexW

Im trying to programatically add rows to a asp:table below are my efforts thus far:-

我试图以编程方式将行添加到下面的 asp:table 是我迄今为止的努力:-

but datarow does not contain a getenumorator, i dont know what this means, can anyone help me out?

但是 datarow 不包含 getenumorator,我不知道这是什么意思,有人能帮我吗?

i know using a repeater is easier but for this page i need a serverside table so im trying to do it this way, also how do i add the column data in the right order to match the tableheaders?

我知道使用中继器更容易,但对于这个页面,我需要一个服务器端表,所以我试图这样做,我如何以正确的顺序添加列数据以匹配表头?

dtEquipment = new dsData.tblEquipmentDataTable();
taEquipment = new dsDataTableAdapters.tblEquipmentTableAdapter();
taEquipment.FillbyUser(dtEquipment);

foreach (DataRow DRow in dtEquipment)
{
    TableRow tRow = new TableRow();
    foreach (DataColumn dCol in DRow)
    {
        TableCell tCell = new TableCell();
        tCell.Text = DRow["AssetNo"].ToString();
        tRow.Cells.Add(tCell);
    }
    tblTest.Rows.Add(tRow);
}

采纳答案by Tim Schmelter

You have to use the DataTable.Rowsproperty and the DataTable.Columnsproperty:

您必须使用该DataTable.Rows属性和该DataTable.Columns属性:

foreach (DataRow DRow in dtEquipment.Rows)
{
    TableRow tRow = new TableRow();
    foreach (DataColumn dCol in dtEquipment.Columns)
    {
        // ...
        tCell.Text = DRow[dCol].ToString();
        // ...
    }
    tblTest.Rows.Add(tRow);
}