Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 14, 2012 02:46 PM by basheerkal
Apr 13, 2012 03:26 PM|LINK
I am Anand. I have a dataset which is filled with a table which contains 10 records (10 rows) and it contains two columns i.e.,
1) ID (for data value field)
2) NAME (for data text field)
I have 10 dropdownlist's in aspx page. I want to bind first record to first dropdownlist and second record to second dropdownlist and so on. Can any one help me out?
Thanks in advance for help.
-- Thanks & regards,
Apr 13, 2012 03:53 PM|LINK
For your scenario, you need to make a common function in that need to loop the dataset with each row, then add the item to the dropdown.
but for that all your dropdowns need to be with same name like DropDown1, DropDown2, in that you can substitute the numeric value with the current row of the datatable
Apr 14, 2012 05:14 AM|LINK
Set DataValueFiled/DataTextField Prperties of each DropDownList = the respective fileld in your table
set DataSource of all DropDownList= your table
DataBind() all DropDownLists
Apr 14, 2012 11:38 AM|LINK
Thanks for your reply. I can implement dropdowns as you said. But if you provide any sample code it would be much better..
Apr 14, 2012 12:17 PM|LINK
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString);
// Table book contains 3 fileds Book_No, Book_names, chp_details
// three drop down lists. DDl1, DDL2, And DDL3 are thre in the form
string str = "select * from books ";
SqlCommand cmd = new SqlCommand(str, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (dt.Rows.Count > 0)
DDL1.DataTextField = "Book_No";
DDL1.DataValueField = "Book_No";
DDL1.DataSource = dt;
DDL2.DataTextField = "Book_names";
DDL2.DataValueField = "Book_names";
DDL2.DataSource = dt;
DDL3.DataTextField = "chp_details";
DDL3.DataValueField = "chp_details";
DDL3.DataSource = dt;
Apr 14, 2012 01:08 PM|LINK
I don't want to bind fields to dropdowns.
What I need is, there are only three fields and every time we'll fetch only 20 records into datatable.
As I told there are 20 dropdowns (same as records fetched) in aspx page, and i need to bind first record to first dropdown and second record to second dropdown and so on..
Apr 14, 2012 02:16 PM|LINK
As I told already try like this,
if (dt.Rows.Count > 0)
for (int i = 1; i <= list.Count; i++)
var droDownList = this.Controls.Find("DDL" + i, true) as DropDownList;
droDownList.DataTextField = dt.Rows[i].ToString();
droDownList.DataValueField = dt.Rows[i].ToString();
Apr 14, 2012 02:46 PM|LINK
Is this you need
(dt.Rows.Count > 0)
If so why DropdownLists. ?