About. Net winform ComboBox Data Binding display

Source: Internet
Author: User

Recently, we developed winform projects and often used ComboBox controls. It may be because winform is rarely used and there was a problem during data binding. It took me a long time to think about it.

The problem is as follows:

ComboBox. Response Se = onetable;

ComboBox. vluemember = "cCode"; // bind the field in onetable

ComboBox. displaymember = "ccusmer"; bind the onetable Field

The onetable table structure is as follows:

CCode ccusmer

1 One

2 two

According to common sense, when you runProgramIn the ComboBox control drop-down menu, the option should be "one, two", But the strange thing is that the displayed drop-down item is "1, 2 ";

Very strange. Later, I finally found out the cause (referencing baiwei technology ):

The datasource attribute of ComboBox is of the object type, but the type of the ilistsource interface must be implemented.

After the datatable value is assigned to this attribute, if the displaymember and valuemember attributes of ComboBox are not specified at this time, the selectvalue attribute of ComboBox will be an instance of system. Data. datarowview.

It is estimated that the getlist () method in the ilistsource interface implemented in datatable should return the defaable view of datatable, that is, an instance of dataview is a set of datarowview.

Because the displaymember and valuemember attributes are not specified, ComboBox directly fills every datarowview instance in dataview into the list. In this way, errors may occur in the selectedindexchanged event processing of ComboBox.

Therefore:

ComboBox. vluemember = "cCode"; // bind the field in onetable

ComboBox. displaymember = "ccusmer"; bind the onetable Field

ComboBox. Response Se = onetable;

First bind the column and then bind the data source. The problem is solved.

The value of ComboBox. text should be "one, two", and the value of ComboBox. selectvlue should be "1, 2"
 

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.