简体   繁体   English

从sqlserver填充数据时在C#中的表中动态添加行

[英]Adding rows dynamically in a table in C# while populating data from sqlserver

I am retrieving data from sqlserver using Dropdown list box selected value and storing it in an arraylist. 我正在使用下拉列表框选定的值从sqlserver检索数据,并将其存储在arraylist中。 Now the problem is how to retieve the arraylist values and display in the table. 现在的问题是如何重新排列arraylist值并在表中显示。

Use it like below 如下使用

    ArrayList myArrayList = new ArrayList();

    //Fill ArrayList values from the SQL Server
    myArrayList.Add("Hello");
    myArrayList.Add("World");

    Table table = new Table();

    foreach (var item in myArrayList)
    {
        TableRow tr = new TableRow();
        TableCell tc = new TableCell();

        //Add Arraylist item into TableCell to display it
        tc.Text = item.ToString();

        tr.Cells.Add(tc);

        table.Rows.Add(tr);


    }

If you are using asp.net mvc you can use something like this: 如果您使用的是asp.net mvc,则可以使用以下代码:

<table>
    @foreach (var item in List) {
    <tr>
        <td>
           item.somethingA
        </td>
        <td>
           item.somethingB
        </td>

    </tr>
}
</table>

etc to create table 等创建表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM