tong
tong

Reputation: 269

what is the easiest way to bind data to 30 dropdownlists?

my question is very simple. here is my scenario:

  1. i have 1 result dataset[named="resultDS" typed="MyDataset"] and 1 datatable[MyDT] inside.
  2. in datatable, i have 10 rows, each row has 3 columns [Column1,Column2,Column3]
  3. on aspx page, i have 30 drowdownlists waiting for above data

The data inside dataTable will be:

Column1 | Column2 | Column3

Method1 | 100 | 0.5

Method2 | 125 | 0.75

Method3 | 80 | 0.4

. . .

Method30 | 200 | 1

currently, i used the simplest way for binding data. something like:

ddl1.SelectedValue = ((MyDataSet.MyDTRow)resultDS.MyDT.Rows[0]).Column1;
ddl2.SelectedValue = ((MyDataSet.MyDTRow)resultDS.MyDT.Rows[0]).Column2;
ddl3.SelectedValue = ((MyDataSet.MyDTRow)resultDS.MyDT.Rows[0]).Column3;

ddl4.SelectedValue = ((MyDataSet.MyDTRow)resultDS.MyDT.Rows[1]).Column1;
ddl5.SelectedValue = ((MyDataSet.MyDTRow)resultDS.MyDT.Rows[1]).Column2;
ddl6.SelectedValue = ((MyDataSet.MyDTRow)resultDS.MyDT.Rows[1]).Column3;
.
.
.
ddl30.SelectedValue = ((MyDataSet.MyDTRow)resultDS.MyDT.Rows[9]).Column3;

Is there any way to bind data to my ddl easier than my current method?

Thank you.

vcha

Upvotes: 0

Views: 143

Answers (2)

Chad
Chad

Reputation: 1562

I would set this up in a Databound control Like a ListView or GridView.

This will allow you to Create the 3 ddls that will be repeated then bind the SelectedItemValue to your column.

I am making some assumptions based on what you have shown that this will work for you. If the ddls are scattered throughout the site then it may not.

Upvotes: 0

Bala R
Bala R

Reputation: 108957

Assuming the last line in you code has Rows[9]

You can try doing it in a loop

for (int i=0; i<30; i++)
{
   DropDownList ddl = (DropDownList) FindControl("ddl"+(i+1));

   switch (i % 3)
   {
      case 0:
          ddl.SelectedValue =  ((MyDataSet.MyDTRow)resultDS.MyDT.Rows[i/3]).Column1;
          break;
      case 1:
          ddl.SelectedValue =  ((MyDataSet.MyDTRow)resultDS.MyDT.Rows[i/3]).Column2;
          break;
      case 2:
          ddl.SelectedValue =  ((MyDataSet.MyDTRow)resultDS.MyDT.Rows[i/3]).Column3;
          break;
    }
}

Upvotes: 1

Related Questions