ComboBox Connection database, display drop-down box options

Source: Internet
Author: User

     ? this article from http://blog.csdn.net/susidian/article/details/7027007 is a place where you learn.     

    ? How to bind the ComboBox control to the database and display the data found from the database in the drop-down box. How to filter the options in the next ComboBox that you can select in the previous ComboBox box. Post the Code:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacetest{ Public classGetData { Public stringProvince {Get;Set; }  Public Override stringToString () {returnProvince; }    }         Public Partial classform1:form {ComboBox combobox_p;        ComboBox Combobox_c;        SqlCommand command;        SqlConnection connection; stringConnectionString ="Data Source = 1111; Initial Catalog = Test; User Id = sa; Password = * * * *";  PublicForm1 () {InitializeComponent ();        Surface (); }        //Create a ComboBox box.         Public voidSurface () {combobox_p=NewComboBox (); Combobox_p.size=NewSize (121, -); Combobox_p.location=NewPoint (186, the);  This.            Controls.Add (combobox_p); Combobox_p.selectedindexchanged+=NewEventHandler (combobox_p_selectedindexchanged);//Select EventCombobox_c=NewComboBox (); Combobox_c.size=NewSize (121, -); Combobox_c.location=NewPoint (313, the);  This.                Controls.Add (Combobox_c); }        Private voidCombobox_p_selectedindexchanged (Objectsender, EventArgs e)  {combobox_C.Items.Clear (); //Remove previous drop-down optionsGetData GetData = (GetData) Combobox_p.selecteditem;//get the currently selected options            stringprovince = getdata.province;//gets the string from the selected option            using(Connection =NewSqlConnection (ConnectionString))//connecting to a database{connection.                Open (); using(Command =connection.   CreateCommand ()) {SqlDataReader reader; //data Read classCommand. Parameters.Add (NewSqlParameter ("Province", province));//Update ProvinceCommand.commandtext ="Select city from City where province = @province;"; using(reader = command.) ExecuteReader ())//returns a SqlDataReader                    {                         while(Reader. Read ())//Data Read                        {                            stringCityName = reader. GetString (reader. GetOrdinal (" City"));//Get serial numberCombobox_C.Items.Add (CityName); }                    }                }            }        }        Private voidForm1_Load (Objectsender, EventArgs e) {            //Open Database            using(Connection =NewSqlConnection (ConnectionString)) {connection.                Open (); using(Command =NewSqlCommand ("select * from Province;", connection))//Query Operations{SqlDataReader Reader= command. ExecuteReader ();//returns a SqlDataReader                     while(Reader. Read ())//Read Data{GetData GetData=NewGetData (); Getdata.province= Reader. GetString (reader. GetOrdinal ("Province"));//gets the string returned by the readCombobox_P.Items.Add (GetData); //add to drop-down box options                    }                }            }        }    }}

ComboBox Connection database, display drop-down box options

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.