Summary of selectedvalue settings in ComboBox in winform (selectedvalue = NULL solution)

Source: Internet
Author: User

In practice, we found that the selectedindex value can be used to obtain selectedvalue when ComboBox is automatically selected; The selectedtext value can be used to leave selectedvalue empty when ComboBox is automatically selected.

That is

This. combobox1.selectedindex = This. combobox1.findstring (room );

In this way, the combobox1.selectedvalue value is obtained.

This. combobox1.selectedtext = room;

In this way, the value of combobox1.selectedvalue is null.

 

The content of the post is as follows:

Recently, when processing a ComboBox value set in a winform in the project, it is strange that when an object matches an object in the datasource set of ComboBox, when selectedvalue is set, the value of selectedvalue is null.

If you use dateset to bind data, selectedvalue returns NULL or datarowview.

 

After repeated practices, we found that the following methods can be used:

Private void setvaluebyitem ()
{
Area A = new area ();
A. area_id = "8 ";
A. area_name = "8th ";
Cblist. selecteditem = ac. findall (delegate (area AR) {return ar. area_id = A. area_id;}) [0];
Lbresult. Text = cblist. selecteditem. GetType (). tostring () + ":" + cblist. selectedvalue. tostring ();
} Two other setting methods: Private void setvaluebyvalue ()
{
Area A = new area ();
A. area_id = "8 ";
A. area_name = "8th ";
Cblist. selectedvalue = A. area_id;
Lbresult. Text = cblist. selectedvalue. GetType (). tostring () + ":" + cblist. selectedvalue. tostring ();
} Complete code:
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. text; using system. windows. forms; namespace winformcombobox {public partial class frmmain: FORM {public frmmain () {initializecomponent () ;}# region members arealists AC; Public arealists gettestarea () {arealists Al = new arealists (); For (INT I = 1; I <11; I ++) {Area A = new area ();. area_id = I. tostring ();. area_name = "no" + I. tostring () + "name"; Al. add (a) ;}return Al ;}# endregion # region events private void frmmain_load (Object sender, eventargs e) {AC = gettestarea (); cblist. datasource = ac; cblist. displaymember = "area_name"; cblist. valuemember = "area_id";} private void btnsetcombobox_click (Object sender, eventargs e) {// setvalue0 (); // setvaluebyvalue (); // setvaluebyitem (); setvaluebytext () ;}# endregion # Region Methods private void setvalue0 () {Area A = new area ();. area_id = "8";. area_name = "8th"; cblist. datasource = gettestarea (); cblist. displaymember = "area_name"; cblist. valuemember = "area_id"; cblist. selectedvalue =. area_id; lbresult. TEXT = cblist. selectedvalue. tostring ();} private void setvaluebyvalue () {Area A = new area ();. area_id = "8";. area_name = "8th"; cblist. selectedvalue =. area_id; lbresult. TEXT = cblist. selectedvalue. getType (). tostring () + ":" + cblist. selectedvalue. tostring ();} private void setvaluebyitem () {Area A = new area ();. area_id = "8";. area_name = "8th"; cblist. selecteditem = ac. findall (delegate (area AR) {return ar. area_id =. area_id;}) [0]; lbresult. TEXT = cblist. selecteditem. getType (). tostring () + ":" + cblist. selectedvalue. tostring ();} private void setvaluebytext () {Area A = new area ();. area_id = "8";. area_name = "8th"; cblist. selectedindex = cblist. findstring (. area_name); lbresult. TEXT = cblist. selectedvalue. getType (). tostring () + ":" + cblist. selectedvalue. tostring () ;}# endregion }} using system; using system. collections. generic; using system. text; namespace winformcombobox {# region area public class area {private string m_area_id; Public String area_id {get {return m_area_id;} set {m_area_id = value;} private string m_area_name; public String area_name {get {return m_area_name;} set {m_area_name = value;} private double m_area_order; Public double area_order {get {return m_area_order ;} set {m_area_order = value ;}}# endregion [serializable] public class arealists: List <area> {private int _ maxitems = 0; Public int maxitems {get {return this. _ maxitems;} set {This. _ maxitems = value ;}}}}

 

 

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.