c# - Auto-Complete in textbox using data from a SQL Server database -
i trying textbox auto-complete when user types in write query. similar how sql server management studio , gives option type arrow down or click on table name or column name. here following code have.
public void loaddata() { var myconnection = new sqlconnection(dbconnectionbox.text); myconnection.open(); autocompletestringcollection namescollection = new autocompletestringcollection(); string query = @"select distinct [name] [information_schema.tables]"; sqlcommand cmd = new sqlcommand(query, myconnection); sqldatareader dr = cmd.executereader(); if (dr.hasrows == true) { while (dr.read()) namescollection.add(dr["name"].tostring()); } dr.close(); myconnection.close(); manualquerybox.autocompletemode = autocompletemode.append; manualquerybox.autocompletesource = autocompletesource.customsource; manualquerybox.autocompletecustomsource = namescollection; } private void manualquerybox_keyup(object sender, keyeventargs e) { loaddata(); }
this code use grab in textbox , execute it.
private void executebtn_click(object sender, eventargs e) { this.clientinfodgv.datasource = null; this.clientinfodgv.rows.clear(); var myconnection = new sqlconnection(dbconnectionbox.text); var manualcmd = new sqlcommand(manualquerybox.text); manualcmd.connection = myconnection; manualcmd.commandtype = commandtype.text; sqldataadapter sqladap = new sqldataadapter(manualcmd); datatable mqrecord = new datatable(); sqladap.fill(mqrecord); clientinfodgv.datasource = mqrecord; }
i have never done autofill before looking around have see asp.net ajax control toolkit, not entirely sure how works. welcome.
update auto-fill code
public void loaddata() { var myconnection = new sqlconnection(dbconnectionbox.text); myconnection.open(); autocompletestringcollection namescollection = new autocompletestringcollection(); string query = @"select distinct [id] [clients]"; sqlcommand cmd = new sqlcommand(query, myconnection); sqldatareader dr = cmd.executereader(); if (dr.hasrows == true) { while (dr.read()) namescollection.add(dr["id"].tostring()); } dr.close(); myconnection.close(); manualquerybox.autocompletemode = autocompletemode.suggestappend; manualquerybox.autocompletesource = autocompletesource.customsource; manualquerybox.autocompletecustomsource = namescollection; }
try changing autocomplete mode suggestappend:
manualquerybox.autocompletemode=autocompletemode.suggestappend;
i don't think need load data every time in key event long load once (perhaps in form load) , specify source of text box.
this might help:https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletemode(v=vs.110).aspx
Comments
Post a Comment