0

我有一个表格,我无法动态填充数据......它只倾向于显示最后一个表格单元格,其余行为空。虽然下拉列表似乎以正确的方式填充。

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);

提前致谢

4

2 回答 2

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);
}
于 2013-08-30T15:48:19.693 回答
2

我添加了一个包含两列的表。每列都有一个dropdownlist来自listitems数据库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);
于 2013-09-02T08:10:23.540 回答