开发者

.net comboboxes with duplicates in the display value

I think I've stumbled upon a bug in the .NET (or the native windows API) concerning combo boxes.

I'll give a specific scenario:

I have two database tables, with a one to many relation ship between the two, a brief description is as follows:

Table: FinSage
Column     Type
SageCode   nvarchar(10) (PK)
Desc       nvarchar(100)

Table: FinCode 
Column     Type 
FinCode    nvarchar(10) (PK) 
Desc       nvarchar(100) 
SageCode   nvarchar(10) (FK)

These are entirely user maintained, with only the PK obviously being constrained to be unique. When creating or editing a record on the FinCode table I have a combobox on the screen which has the display member bound to the 'Desc' column and the value member bound to SageCode.

The problem arises when there are duplicate entries in the 'Desc' column, which is quite common under the expected use of the system. It seems the .net combo boxes does a reverse lookup using the display value to get the value member. So when the combo box gains focus it will change the value to the first occurrence of the display member.

I understand that in theory, if there are duplicates in the display value then the combo box effectively has no use as the user doesn't know which value they have selected. However as we know users are not perfect and so they will and do enter the same descriptive value for a code.

I currently get around this by combining the code and the description, however my boss wants them to be able to edit this value by using either the code or the description (as some uses prefer to use one or the other). I tried using two combo boxes bound to the same binding source but saw this problem arise.

Apologies for the verbose description, its not really a problem that can be summed up in code.

On a slightly related note, the combo boxes that Access use don't have this bug. But I believe Access is comparable to WPF in that it draws all its own UI components rather than usi开发者_开发技巧ng the Win32 API.

Is there not a way of 'forcing' a native combo box to find the correct item in the list based on the associated value or was the concept of value-description pair developed after Microsoft designed their native combo box?

Thank you for any insights

Marlon

*edit*

The answer below prompted me to quickly dev up a test application and I'm not getting the same behaviour unless the combobox is inside a datagridview (which is unrelated to the original question).

Sorry Microsoft, I would delete this question but I cant now, ah well, mistakes are good.


The solution is set DropDownList in the DropDownStyle property of the designer


ComboBoxes and ListBoxes do have value-description pairs in the form value(index)-Description(text).

Therefore if the combobox includes to of the same description they are still unique as they don't have the same index.

I'd say rather than there being a bug in .net there's a bug in your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜