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"