c# - How to bind ComboBox from ArrayList Values in asp.net? -
but not fill properly
i have mention such table , code image
code :
datatable dtusertype = new common().getusertype(true); datatable dtusertype 1= new common().getusertypebyuser(true); arraylist lstusertypeid =new arraylist(); lstusertypeid.addrange(dtusertype1.rows[0]["usertypeid"].tostring().split(',')); datatable dtdownlist = new datatable(); dtdownlist.columns.add("usertypeid"); dtdownlist.columns.add("usertype"); datarow dr; foreach (string s in lstusertypeid) { (int = 0; < dtusertype.rows.count; i++) { dr = dtdownlist.newrow(); //list of superior list if(s.tostring() != dtusertype.rows[i]["usertypeid"].tostring()) { dr["usertype"] = dtusertype.rows[i]["usertype"].tostring(); dr["usertypeid"] = dtusertype.rows[i]["usertypeid"].tostring(); dtdownlist.rows.add(dr); } } } cmbuser.datasource = dtdownlist; cmbuser.datatextfield = "usertype"; cmbuser.datavaluefield = "usertypeid"; cmbuser.databind();
in general:
- don't split main table sub tables 1 column each - why there columns in table
- when querying data - use linq
dont use datatable when possible - observer how many casting operations posted in answer (in case use typed table common)
datatable common = new common(); // id, usertype, usertypeid var sections = common.where(c => c.usertype.tostring() == "b").first() .tostring().split(','); datatable dtdownlist = new datatable(); dtdownlist.columns.add("usertypeid"); dtdownlist.columns.add("usertype"); datarow dr; foreach (string id in sections) { (int = 0; < dtusertype.rows.count; i++) { if(convert.toint16(id) == convert.toint16(common.rows[i]["id"])) { dr = dtdownlist.newrow(); dr["usertype"] = dtusertype.rows[i]["usertype"].tostring(); dr["usertypeid"] = dtusertype.rows[i]["usertypeid"].tostring(); dtdownlist.rows.add(dr); } } } cmbuser.datasource = dtdownlist; cmbuser.datatextfield = "usertype"; cmbuser.datavaluefield = "usertypeid"; cmbuser.databind();
Comments
Post a Comment