c# - How to set the value of dropdownlist from data table? -
i'm trying bind dropdownlist data table, data table contain departmentid, , departmnentname. binding successful, how set value of items?
dt = objdeparment.selectall().tables[0]; foreach (datarow dr in dt.rows) { dropdownlist1.items.add(dr[1].tostring()); //binding dropdownlist department names }
don't add string, add listitem
(which exposes more useful constructors this):
dropdownlist1.items.add(new listitem(dr[1].tostring(), dr[0].tostring())); // ^^--text ^^--value
(assuming "value" want in dr[0]
, use whatever holds actual value code)
you bind control directly datatable
instead of adding items in loop. this:
dropdownlist1.datasource = objdeparment.selectall().tables[0]; dropdownlist1.datatextfield = "some column"; dropdownlist1.datavaluefield = "another column"; dropdownlist1.databind();
Comments
Post a Comment