Bind arraylist (namespace system. Collections)
View plaincopy to clipboardprint?
- Arraylist al1 = new arraylist ();
- Al1.add (New dictionaryentry ("Y", "Military Product "));
- Al1.add (New dictionaryentry ("N", "minipin "));
- Cbisarmy. datasource = al1;
- Cbisarmy. displaymember = "value ";
- Cbisarmy. valuemember = "key ";
Arraylist al1 = new arraylist (); al1.add (New dictionaryentry ("Y", ""); al1.add (New dictionaryentry ("N", ""); cbisarmy. datasource = al1; cbisarmy. displaymember = "value"; cbisarmy. valuemember = "key ";
The ComboBox bound with datesource cannot use the items. Add method recently used the ComboBox control. To briefly summarize its usage (bind, add options, and clear ):
I. ComboBox binding
View plaincopy to clipboardprint?
- Oledb = new oledb ();
-
- Private void form1_load (Object sender, eventargs E)
- {
- Datatable dt = oledb. filldatatable ("select ID, name from Table1"); // The function returns the SQL-related datatable
- Combobox1.datasource = DT;
- Combobox1.displaymember = "name"; // display content
- Combobox1.valuemember = "ID"; // value corresponding to the option
- }
Oledb = new oledb (); Private void form1_load (Object sender, eventargs e) {datatable dt = oledb. filldatatable ("select ID, name from Table1"); // The function returns the SQL-related datatable combobox1.datasource = DT; combobox1.displaymember = "name"; // The displayed content combobox1.valuember = "ID "; // value corresponding to the option}
2. Add options for ComboBox
The ComboBox bound with datesource cannot use items. the add method adds options, which can only be implemented by modifying the able. For example, to add the option "--- all ---" to the combox1 implemented above, you can do this:
View plaincopy to clipboardprint?
- Private void form1_load (Object sender, eventargs E)
- {
- Datatable dt = oledb. filldatatable ("select ID, name from Table1"); // The function returns the SQL-related datatable
- Datarow DR = DT. newrow ();
- Dr ["ID"] = 0;
- Dr ["name"] = "--- all ---";
- DT. rows. insertat (DR, 0); // use DT. rows. add (DR) only adds the options to the end, and uses DT. rows. insertat (DR, 0) can insert the added options to the corresponding positions.
-
- Combobox1.datasource = DT;
- Combobox1.displaymember = "name"; // display content
- Combobox1.valuemember = "ID"; // value corresponding to the option
- }
Private void form1_load (Object sender, eventargs e) {datatable dt = oledb. filldatatable ("select ID, name from Table1"); // The function returns the SQL-related datatable datarow DR = DT. newrow (); Dr ["ID"] = 0; Dr ["name"] = "--- all ---"; DT. rows. insertat (DR, 0); // use DT. rows. add (DR) only adds the options to the end, and uses DT. rows. insertat (DR, 0) can insert the added options to the corresponding position combobox1.datasource = DT; combobox1.displaymember = "name"; // The displayed content combobox1.valuemember = "ID "; // value corresponding to the option}
3. Clear the options in ComboBox
If the options in ComboBox are handwritten, use combobox1.items. clear (); method to achieve this, and the ComboBox bound to datasource cannot use this method. In fact, in this case, clearing the option is also very simple. Use combobox1.datasource = NULL; you can solve the problem.