2013-08-30 65 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);