Set SelectedValue in ComboBox under asp.net WinForm

Source: Internet
Author: User

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 ();
A. area_id = I. tostring ();
A. area_name = "" + I. tostring () + "name ";
Al. add ();
}
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 ();
A. area_id = "8 ";
A. area_name = "8th ";

Cblist. datasource = gettestarea ();
Cblist. displaymember = "area_name ";
Cblist. valuemember = "area_id ";
Cblist. selectedvalue = a. area_id;
Lbresult. text = cblist. selectedvalue. tostring ();
}

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 ();
}

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 ();
}

Private void setvaluebytext ()
{
Area a = new area ();
A. area_id = "8 ";
A. area_name = "8th ";

Cblist. selectedindex = cblist. findstring (a. 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 ;}}
}
}

Related Article

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.