rawatdeepesh
rawatdeepesh

Reputation: 584

to add dropdown in a table dynamically

I have a table which i cant fill up with data dynamically ... it only tends to show the last table cell the rest rows are empty.The dropdowndownlist seems to popuate in the right way though.

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

Thanks in Advance

Upvotes: 0

Views: 4307

Answers (2)

rawatdeepesh
rawatdeepesh

Reputation: 584

i added a table with two columns.Each column has a dropdownlist with the listitems from a SQL database and an Excel file.

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

Upvotes: 2

Suraj Singh
Suraj Singh

Reputation: 4069

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

You are iterating over sqlcolumn but your sqlcolumn is empty. It does not have any values to fill up your DropDownList. Here you have defined your string array of a particular length but it does not contains any value. (Hope I am on right direction as I can see only this much of your code).

You can directly fill your DropDownList from a datatable.

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

This will work

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

Upvotes: 4

Related Questions