Summary of ComboBox binding in winform

Source: Internet
Author: User

1. datatable

Bind the data table directly. You only need to set the datasource, displaymember, and valuemember attributes.

Code
This . Cmbconsumesupermarket. datasource = Dtsupermarket;
This . Cmbconsumesupermarket. displaymember =   " Name " ; This . Cmbconsumesupermarket. valuemember =   " ID " ; This . Cmbconsumesupermarket. selectedindex =   0 ;

You can use the following method to obtain the corresponding ID and name, which can basically meet the business requirements.

Stringtools. objecttoint ( This . Cmbconsumesupermarket. selectedvalue );
Stringtools. objecttostr ( This . Cmbconsumesupermarket. selectedtext );

However, the problem above is that the first item is displayed by default after ComboBox is bound, but a prompt option is required, and I have not found any good implementation method.

Some people use ComboBox. selectedindex =-1 or ComboBox. text or initialize ComboBox. items to set an item as the initial item or ComboBox. dropdownstyle, but I have not done so here.

This should have achieved effect A, but the above can only achieve effect B, so the above does not meet the requirements.

Effect a effect B

 

2. ComboBox. Items. Add

At the beginning, I thought that the listitem attribute like Asp.net could be used, but items only had a few simple attributes. Fortunately, add (Object item), so I can only do this in the object.Article.

So we have a new object for the item to be bound, and then rewrite tostring.

Because there are many similar ComboBox controls on the entire page, we can simply abstract them and then implement effect B conveniently. The specific implementation method is as follows:

Code
Using System. Data;
Using System. Windows. forms;

Namespace Blackcore. App. Method
{
// Abstract class databindcontrols introduces abstract method databindcombobox (......)
Public   Abstract   Class Databindcontrols
{
///   <Summary>
/// Bind ComboBox
///   </Summary>
///   <Param name = "CMB"> ComboBox control </Param>
///   <Param name = "isinsertdefaultitem"> Whether to insert a default option for this control and select it by default </Param>
///   <Param name = "DT"> Datatable to be bound </Param>
///   <Param name = "selectedtext"> Displaymember) </Param>
///   <Param name = "selectedvalue"> Valuemember </Param>
Public   Abstract   Void Databindcombobox (ComboBox CMB, Bool Isinsertdefaultitem, datatable DT, String Selectedtext, String Selectedvalue );
}
}

Implement Abstraction

Code
Using System. Data;
Using System. Windows. forms;
Using Blackcore. financiallibrary;

Namespace Blackcore. App. Method
{
// Implementation Abstraction
// Class databindcontrolsimplement override databindcombobox and provide a specific implementation.
// Since no abstract member exists in databindcontrolsimplement, you can (but not necessarily) Declare databindcontrolsimplement as a non-abstract class.
Public   Class Databindcontrolsimplement: databindcontrols
{
Public   Override   Void Databindcombobox (ComboBox, Bool Isinsertdefaultitem, datatable, String Selectedtext, String Selectedvalue)
{
If (Datatable ! =   Null   && Datatable. Rows ! =   Null   && Datatable. Rows. Count >   0 )
{
If (ComboBox. Items. Count >   0 )
{
ComboBox. Items. Clear ();
}
Int I =   1 ;
Foreach (Datarow In Datatable. Rows)
{
// ComboBox. selectedtext = stringtools. objecttostr (datarow [selectedtext]). Trim ();
// ComboBox. selectedvalue = stringtools. objecttoint (datarow [selectedvalue]). tostring ();

// blackcore. bll. specified almanage. fmproject bllproject = new blackcore. bll. specified almanage. fmproject ();
// blackcore. model. specified almanage. fmproject modelproject = new blackcore. model. specified almanage. fmproject ();
// modelproject = bllproject. getModel (stringtools. objecttoint (datarow ["ID"]);

//In this way, only selectedtext is used, but selectedvalue is not used.
//ComboBox. Items. Add (stringtools. objecttostr (datarow [selectedtext]). Trim ());

// Any types of objects that can be stored in ComboBox, rather than strings. The text box generated by override tostring () will be displayed.
// In this way, you can implement selectedtext, selectedvalue, or more required attributes.
ComboBox. Items. Add ( New Comboboxitemtextvalue (stringtools. objecttoint (datarow [selectedvalue]). tostring (), stringtools. objecttostr (datarow [selectedtext]);
}
If (Isinsertdefaultitem)
{
ComboBox. Items. insert ( 0 , " Select " );
}
ComboBox. selectedindex =   0 ;
}
}

Public ClassComboboxitemtextvalue
{
Public StringSelecttext;
Public StringSelectvalue;

Public Comboboxitemtextvalue ( String _ Selectvalue, String _ Selecttext)
{
Selectvalue = _ Selectvalue;
Selecttext = _ Selecttext;
}
Public   Override   String Tostring ()
{
Return Selecttext;
}
}

}
}

ComboBox binding

Code
Databindcontrolsimplement implement =   New Blackcore. App. method. databindcontrolsimplement ();

Implement. databindcombobox (This. Searchcmbconsumemarket,True, Bllmarket. getlist (""). Tables [0],"Name","ID");

Get ComboBox

Code
If (Stringtools. objecttoint (searchcmbconsumemarket. selectedindex) ! =   0 )
{
Databindcontrolsimplement. comboboxitemtextvalue comboitem =  
(Databindcontrolsimplement. comboboxitemtextvalue) This . Searchcmbconsumeproject. selecteditem;
String Selectedtext = Comboitem. selecttext;
Int Selectedvalue = Comboitem. selectvalue;
}

 

 

I am a beginner in winform development. The above content is for personal purposes and can be used as needed. If it is incorrect, please help us with correction. Thank you very much! Blackcore!

 

 

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.