2013-08-30 62 views
0

我有一个表格,我无法动态地填充数据......它只倾向于显示最后一个表格单元格,剩下的行是空的。虽然dropdowndownlist似乎以正确的方式进行了扩展。动态地在表格中添加下拉列表

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

由于提前

+0

你真的应该给'l'一个更好的变量名,一会儿我以为是'1'。只是为了清楚起见... – Damon

回答

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); 
} 
+0

感谢您的回复Suraj.I先生添加的代码用于填充sqlcolumn.I检查页面源代码运行的代码之后.... '

​​​​​​​​​​​​地址
' 只有表的最后一行充满control.The其余TD是空 – rawatdeepesh

+0

@rawatdeepesh我得到了一个解决方案,虽然它不是一个干净的BU检查我的答案我已经编辑它。 –

+0

非常感谢Suraj,它的工作......如果你觉得它的权利,请点击打勾来接受我的答案。 – rawatdeepesh

2

我添加了一个表有两个columns.Each列有dropdownlistlistitemsSQL数据库和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);