C # add data after the ComboBox data source is bound

Source: Internet
Author: User

After the ComboBox is bound, a series of "memory protection" errors may occur in dynamic data changes, and some things written by foreigners are accidentally discovered.

That is to say, in the case of static binding, the ComboBox option is added without changing the data source. Here, an error is returned when ComboBox. Items. Add is used.

The content of the test table is as follows:

Table testtable: seqno content
AS1 Zhang San
As2 Li Si
33. as11
55 as22
Ding ad33

 

The Code is as follows:

Private void form1_load (Object sender, eventargs e) <br/>{< br/> string conditionstr = "as"; <br/> This. combobox1.textchanged-= new system. eventhandler (this. combobox1_textchanged); <br/> string str1 = "select content, seqno from testtable where (content like '"+ conditionstr +" %' or seqno like '"+ conditionstr +" %') order by content "; <br/> tempdatatable = getdata (str1); </P> <p> combobox1.displaymember = "content"; <br/> combobox1.datasource = tempdatatable; </P> <p> combobox1.autocompletemode = autocompletemode. suggestappend; <br/> combobox1.autocompletesource = autocompletesource. listitems; <br/> This. combobox1.textchanged + = new system. eventhandler (this. combobox1_textchanged); <br/>}</P> <p> Public static datatable getcomboboxeddatatable (datatable olddatatable, string valuecolumn, string textcolumn, string toprowvalue, string toprowtext) <br/>{< br/> datatable newdatatable = new datatable (); <br/> newdatatable. columns. add (valuecolumn); <br/> newdatatable. columns. add (textcolumn); </P> <p> foreach (datarow olddr in olddatatable. rows) <br/>{< br/> datarow newdr = newdatatable. newrow (); <br/> newdr [0] = olddr [valuecolumn]. tostring (); <br/> newdr [1] = olddr [textcolumn]. tostring (); <br/> newdatatable. rows. insertat (newdr, newdatatable. rows. count); <br/>}</P> <p> // Add your 'select an item' option at the top <br/> datarow DR = newdatatable. newrow (); <br/> Dr [0] = toprowvalue; <br/> Dr [1] = toprowtext; <br/> newdatatable. rows. insertat (DR, 0); </P> <p> return newdatatable; <br/>}</P> <p> private void combobox1_textchanged (Object sender, eventargs E) <br/>{< br/> This. combobox1.textchanged-= new system. eventhandler (this. combobox1_textchanged); <br/> combobox1.datasource = getcomboboxeddatatable (tempdatatable, "content", "seqno", "Wang Wu", "as3"); <br/> This. combobox1.textchanged + = new system. eventhandler (this. combobox1_textchanged); <br/>}</P> <p> private datatable getdata (string strsql) <br/>{< br/> datatable dtdata = new datatable (); <br/> using (sqlconnection thisconnection = new sqlconnection (<br/> @ "Data Source = (local); initial catalog = ave661; Integrated Security = true ")) <br/>{< br/> using (sqldataadapter thisadapter = new sqldataadapter (<br/> strsql, thisconnection) <br/>{< br/> thisadapter. fill (dtdata); <br/>}< br/> thisconnection. close (); <br/>}< br/> return dtdata; <br/>}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.