? 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