动态地在表中添加下拉列表

时间:2013-08-30 11:54:06

标签: c# asp.net drop-down-menu

我有一个表格,我无法动态地填充数据......它只会显示最后一个表格单元格,其余的行是空的。虽然dropdownndownlist似乎以正确的方式填充。

DataTable dtt1 = (DataTable)Session["dbasecolumns"];
    int l = dtt1.Columns.Count;
    string[] sqlcolumn = new string[l];


    ***for (int j = 0; j < dtt1.Columns.Count; j++)
    {
        sqlcolumn[j] = dtt1.Columns[j].ColumnName;
    }
    Session["excel"] = sqlcolumn;***


DropDownList drd = new DropDownList();
    foreach (String colname in sqlcolumn)
    {

        drd.Items.Add(colname);
    }

    Table mytable = new Table();
    mytable.Visible = true;

    for (int rowctr = 0; rowctr < sqlcolumn.Length; rowctr++)
    {
        TableRow myrow = new TableRow();
        mytable.Rows.Add(myrow);


        for (int cellctr = 0; cellctr < 1; cellctr++)
        {
            TableCell mycell = new TableCell();
            mycell.Controls.Add(drd);
            myrow.Cells.Add(mycell);
        }
      ***mytable.Rows.Add(myrow);***
     }

    Panel1.Controls.Add(mytable);

先谢谢

2 个答案:

答案 0 :(得分:4)

DataTable dtt1 = (DataTable)Session["dbasecolumns"];
int l = dtt1.Columns.Count;
string[] sqlcolumn = new string[l];
DropDownList drd = new DropDownList();
foreach (String colname in sqlcolumn)
{
    drd.Items.Add(colname);
}

您正在迭代sqlcolumn,但您的sqlcolumn为空。它没有任何值来填充您的DropDownList。在这里,您已定义了特定长度的字符串array,但它不包含任何值。 (希望我的方向正确,因为我只能看到你的代码)。

您可以直接从数据表填充DropDownList

DataTable dtt1 = (DataTable)Session["dbasecolumns"];   

DropDownList drd = new DropDownList();
for (int i = 0; dtt1 .Rows.Count > i; i++)
{
    dhgdh.Items.Add(dt.Rows[i]["dbasecolumns"].ToString());
}

这将有效

for (int rowctr = 0; rowctr <= sqlcolumn.Length; rowctr++)
{
    TableRow myrow = new TableRow();
    mytable.Rows.Add(myrow);

    // for (int cellctr = 0; cellctr <= 1; cellctr++)
    //{
    DropDownList drd = new DropDownList();
    foreach (String colname in sqlcolumn)
    {
        drd.Items.Add(colname);
    }

    TableCell mycell = new TableCell();
    mycell.Controls.Add(drd);
    myrow.Cells.Add(mycell);
    //}
    mytable.Rows.Add(myrow);
}

答案 1 :(得分:2)

我添加了一个包含两列的表格。每列都有一个dropdownlistlistitems来自SQL数据库,Excel文件。

 for (int rowctr = 0; rowctr <= sqlcolumn.Length; rowctr++)
    {
          mycell = new TableCell();
        TableCell mycell1 = new TableCell();

         for (int cellctr = 0; cellctr < 1; cellctr++)
         {
            DropDownList drd = new DropDownList();
            DropDownList drd1 = new DropDownList();
            foreach (String colname in sqlcolumn)
            {
                drd.Items.Add(colname);                    
            }

            foreach (string colnames in excel)
            {
                drd1.Items.Add(colnames);
            }                
            TableRow myrow = new TableRow();           

            mycell.Controls.Add(drd);
            mycell1.Controls.Add(drd1);            
            myrow.Cells.Add(mycell);
            myrow.Cells.Add(mycell1);
            mytable.Rows.Add(myrow);
}
Panel1.Controls.Add(mytable);