c# - Listing matching items in a combo box while entering a text? -
i have combobox
in form , while user typing text in box, if there matching items in database text typed, should listed in combobox
. user can notified if item being entered in database. i'm trying achieve follows.
when text entered in combo
fire event. listening event, presenter
matching user list database , list set datasource
of combobox
follows...
but why not displayed in combo? please let me know issue in code!
form
public partial class frmuser : form { // 2 seperate , set properties used same combo box list set datasource. public string username { get{return cmbusername.text;} } public ienumerable<user> userlist { set { cmbusername.datasource = value; } } private void cmbusername_textchanged(object sender, eventargs e) { onchangetext(sender, e); } }
presenter
class userpresenter { private void wireupevents() { _view.onchangetext += new eventhandler(_view_onchangetext); } private void _view_onchangetext(object sender, eventargs e) { showmatchingusers(); } private void showmatchingusers() { var userlist = _dataservice.getusers(_view.username ); //edit _veiew.userlist = userlist; //edit } }
dataservice
public ienumerable<user> getusers(string userid) { string selectstatement = "select user_id [user] user_id '@userid %'"; list<user> userlist = new list<user>(); using (var sqlconnection = new sqlconnection(db.connectionstring)) using (var sqlcommand = new sqlcommand(selectstatement, sqlconnection)) { sqlcommand.parameters.add("@userid", sqldbtype.varchar).value = userid ; sqlconnection.open(); using (sqldatareader datareader = sqlcommand.executereader()) while (datareader.read()) { userlist.add( new user { userid = (string) datareader["user_id"] }); } return userlist; } }
i can't tell exactly how change code because missing quite few details, events , classes , possible nesting states of classes , objects.
but error is, noted, fact resulting ienumerable<user>
which getusers
brings thrown away.
you wrote should used datasource
of combobox
via property userlist
in fact hiding property declaring new
list<user> userlist = new list<user>();
locally in getusers
. ok, albeit little confusing; (i suggest call different newuserlist
;) still need set real userlist
returned result set somewhere!
when patched (missing important info on actual code)
private void cmbusername_textchanged(object sender, eventargs e) { userlist = _dataservice.getusers("_view.username"); ///onchangetext(sender, e); re-instate own wiring! }
a dummy value showed fine in combobox..
you should not use code stay set up! changing
_dataservice.getusers(_view.username );
to
userlist = _dataservice.getusers(_view.username );
seems direct solution, have watch way connect innner , outer class variables..
Comments
Post a Comment