C# 以编程方式创建表

C# 以编程方式创建表,c#,asp.net,asp.net-mvc,html-table,C#,Asp.net,Asp.net Mvc,Html Table,我已经搜索了相当长的一段时间,通过使用C编写代码来创建下表。我发现在向和中添加a很困难。这是表格的外观: <table id="mytable" border="1" cellpadding="10" cellspacing="0" > <caption>myCaption</caption> <thead> <tr> <td>&nbsp;</td

我已经搜索了相当长的一段时间,通过使用C编写代码来创建下表。我发现在向和中添加a很困难。这是表格的外观:

<table id="mytable" border="1" cellpadding="10" cellspacing="0" >
     <caption>myCaption</caption>
     <thead>
         <tr>
             <td>&nbsp;</td>
             <th scope="col">myHeader1</th>
             <th scope="col">myHeader2</th>
             <th scope="col">myHeader3</th>
         </tr>
     </thead>
     <tbody>
         <tr>
             <td>2</td>
             <td>3</td>
             <td>1</td>
         </tr>
     </tbody>
 </table>

非常感谢您的帮助。

创建一个强类型视图,其中包含所绑定内容的列表,并使用@foreach循环:


我用MVC做过类似的事情。下面是一个示例代码。希望这将有助于得到一个想法

@{int count = Model.HeadingsList.Count;}
<table>
    <thead>
        <tr>
            @foreach (string heading in Model.HeadingsList)
            {
                <th>@heading</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (string row in Model.rowsList)
        {
            string[] cellValues = Array.ConvertAll(row.Split(','), p => p.Trim());
            if (count != cellValues.Count()) { return; } 
            <tr>
                @for (int i = 0; i < @count; i++) 
                {
                            <td>@cellValues[i]</td>
                }
            </tr>
        }
    </tbody>
</table>
我有一个如下所示的行列表。 数组[0]['cell1','cell2','cell3'…]
数组[1]['cell1','cell2','cell3'…]

你好,Dave,这是MVC…你的模型看起来怎么样?@RVP供参考检查
@{int count = Model.HeadingsList.Count;}
<table>
    <thead>
        <tr>
            @foreach (string heading in Model.HeadingsList)
            {
                <th>@heading</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (string row in Model.rowsList)
        {
            string[] cellValues = Array.ConvertAll(row.Split(','), p => p.Trim());
            if (count != cellValues.Count()) { return; } 
            <tr>
                @for (int i = 0; i < @count; i++) 
                {
                            <td>@cellValues[i]</td>
                }
            </tr>
        }
    </tbody>
</table>