Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlDataAdapter da1 = new SqlDataAdapter("select * from vehicle_category", con);
                DataTable dt1 = new DataTable();
                da1.Fill(dt1);
                ddlvnameri1.DataSource = dt1;
                ddlvnameri1.DataTextField = "vehicle_name";
                ddlvnameri1.DataValueField = "vehicle_id";
                ddlvnameri1.DataBind();
                ddlvnameri2.DataSource = dt1;
                ddlvnameri2.DataTextField = "vehicle_name";
                ddlvnameri2.DataValueField = "vehicle_id";
                ddlvnameri2.DataBind();


here i have filled data in only 2 dropdown list. i have to fill 10 dropdown list.
so how to write for loop for dis
Posted
Updated 16-Mar-13 0:08am
v2

Try with the following:

C#
DropDownList[] arr = { ddlvnameri1, ddlvnameri2, ... };  //list of all dropdownlists.
foreach (var d in arr)
{
    d.DataSource = dt1;
    d.DataTextField = "vehicle_name";
    d.DataValueField = "vehicle_id";
    d.DataBind();
}
 
Share this answer
 
Comments
Member 9671810 16-Mar-13 13:17pm    
not working .. :(
Chinmaya C 16-Mar-13 13:20pm    
are you getting any error message?
Member 9671810 16-Mar-13 13:28pm    
no the data is not getting bind

DropDownList[] arr = {ddlvnameri1, ddlvnameri2 };

foreach (var d in arr)
{
DataTable dt = new DataTable();
d.DataSource = dt;
d.DataTextField = "vehicle_name";
d.DataValueField = "vehicle_id";
d.DataBind();
}

i wrote dis code for the code what i have asked.
Chinmaya C 16-Mar-13 13:32pm    
actually you are creating a blank DataTable in the for loop, which is getting binded with dropdowns, so no data is displayed...

I have modified your cde... please try with the following ...

SqlDataAdapter da1 = new SqlDataAdapter("select * from vehicle_category", con);
DataTable dt = new DataTable();
da1.Fill(dt);

DropDownList[] arr = {ddlvnameri1, ddlvnameri2 };
foreach (var d in arr) {
d.DataSource = dt;
d.DataTextField = "vehicle_name";
d.DataValueField = "vehicle_id";
d.DataBind();
}
Member 9671810 16-Mar-13 13:37pm    
hehe.. i forgot that i have to fill datatable...

thanx...a lot...... :P
foreach (Control ctrl in this.Controls)<br />
            {<br />
             <br />
               if (ctrl.GetType().Name == "ComboBox")<br />
                {<br />
                    ComboBox cmb = (ComboBox)ctrl;<br />
                    cmb.DataSource = dt1;<br />
                cmb.DataTextField = "vehicle_name";<br />
                cmb.DataValueField = "vehicle_id";<br />
                cmb.DataBind();<br />
                }<br />
            }<br />


Use this if you want to fill same data in all you ddl on a form..
else use some condition like name checking etc..
 
Share this answer
 
Comments
Member 9671810 16-Mar-13 6:45am    
why to use combo box
aankur81 16-Mar-13 7:42am    
Drop down list termed as combobox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900