Form1 This drop-down box for the shipping unit ComboBox1 has the name field of the Database test table already bound, with a lot of organization names
For example: A company, B company ...
1, Form1 ComboBox1 first bind the Database data table test
using(Sqliteconnection con =Newsqliteconnection (data_source)) {con. Open (); using(Sqlitecommand cmd =NewSqlitecommand ()) {cmd. Connection=con; Cmd.commandtext=string. Format ("SELECT * FROM Test t"); introws =cmd. ExecuteNonQuery (); Sqlitedataadapter SDA=Newsqlitedataadapter (CMD); DataSet DS=NewDataSet (); Sda. Fill (DS); DataTable DT= ds. tables[0]; This. Combobox1.datasource =DT; //Delete DelegateCombobox1.selectedindexchanged-=NewEventHandler (combobox1_selectedindexchanged);//This is superfluous. This. Combobox1.displaymember ="name";//drop-down box to display the fields of the database This. Combobox1.valuemember ="ID"; The ID of the table that corresponds to the field of the displayed database This. Combobox1.selectedindex =-1;//First comboBox1 shown as empty//Add DelegateCombobox1.selectedindexchanged + =NewEventHandler (combobox1_selectedindexchanged);This is superfluous.
} }
2. Form Form2 The drop-down box to assign values to the form Form1:
Let this drop-down box property modifies to public, in Form2 FHDW this variable to Form1 drop-down box
Form1 z = new Form1 (); z.combobox1.text = FHDW;
Form2 assignment has been completed, can be seen in the Form1, the drop-down box has been shown to have a value, but only has a value, and is not in the selected state, you cannot use it SelectedValue
3. How to make this value selected after assignment:
In Form1 's comboBox1 has been bound to many units, looking for this passed over the text value,
stringFHDW =""; intFhdw_index = This. combobox1.findstring (Combobox1.text);//return 0 after finding This. Combobox1.selectedindex =Fhdw_index; SelectedIndex equals 0, which is the equivalent of selectingif(Combobox1.selectedindex = =-1)//Verify that you have selected {MessageBox.Show ("Please select a shipping unit"); return; } FHDW= ComboBox1.SelectedValue.ToString ();//Because it is selected, it can be used SelectedValue
Other forms are assigned to the ComboBox and the assigned value is selected (Gets selectedindex based on text)