VS 2005 - ComboBox does not reset to blank on moving to record withempty value.

M

Matt

I'm not entirely sure how to describe this issue. I have a number of
ComboBoxes in my application which have their text properties bound to a
field in a data set. The items loaded in the ComboBox are not data bound
(they just use the built in collection property of the ComboBox), and
they are all set to use the DropDownList style. When moving from record
to record via a BindingNavigator or a DataGridView (in master/detail
format), the text ComboBox appropriately reflects the value of the field
in the database, so long as it is not null. However, if you pass from a
record which contained a non-null value in the field represented by the
ComboBox to one which contains a null value in the same field, the
ComboBox simply continues to display the last non-null value it held.

For example: I want users to be able to select a Prefix/Salutation (Mr.,
Mrs., etc...) for Members being stored in an application. If you move
from a record with prefix filled in (eg., Mr. John Smith), to one
without (eg., Jane Doe), the ComboBox will continue to display the "Mr."
prefix. If, however, you move to another record which contains a prefix
(eg., Dr. Robert Smith), it will correctly display the new value.

Is this intended behavior, and if so, is there an effective way to get
around it?

Thanks,
Matt
 
Y

Yuan Ren[MSFT]

Hi Matt,

Thanks for your posting!

I have performed testing under your description, but I can't repro the
problem. So could you please supply a simple demo to help me narrowing down
the current issue? Thanks for your understanding! You can send your project
as zip format to my alias -- (e-mail address removed) (remove .online).

I'm looking forward your mail!

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
 
B

Bart Mermuys

Hi,

Matt said:
I'm not entirely sure how to describe this issue. I have a number of
ComboBoxes in my application which have their text properties bound to a
field in a data set. The items loaded in the ComboBox are not data bound
(they just use the built in collection property of the ComboBox), and they
are all set to use the DropDownList style. When moving from record to
record via a BindingNavigator or a DataGridView (in master/detail format),
the text ComboBox appropriately reflects the value of the field in the
database, so long as it is not null. However, if you pass from a record
which contained a non-null value in the field represented by the ComboBox
to one which contains a null value in the same field, the ComboBox simply
continues to display the last non-null value it held.

For example: I want users to be able to select a Prefix/Salutation (Mr.,
Mrs., etc...) for Members being stored in an application. If you move from
a record with prefix filled in (eg., Mr. John Smith), to one without (eg.,
Jane Doe), the ComboBox will continue to display the "Mr." prefix. If,
however, you move to another record which contains a prefix (eg., Dr.
Robert Smith), it will correctly display the new value.

Is this intended behavior, and if so, is there an effective way to get
around it?

I can reproduce it, it appears that the (Data)Binding converts DBNull.Value
into an empty string for string properties but ComboBox.Text doesn't do
anything when set to an empty string and mode is DropDownList.

Workarounds:
Since the ComboBox list isn't databound, you could add an item like "---" to
the Items to indicate nothing is selected and then set ComboBox -
properties - (DataBindings) - (Advanced) - Text - NullValue to "---"
(without the quotes).

-Or attach an event to the Format event of the (Data)Binding and change
DBNull.Value into null:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

AddHandler ComboBox1.DataBindings("Text").Format, AddressOf
OnComboBox1Format
End Sub

Private Sub OnComboBox1Format(ByVal Sender As Object, ByVal e As
ConvertEventArgs)

If (e.Value Is DBNull.Value) Then e.Value = Nothing
End Sub


HTH,
Greetings
 
M

Matt

Thanks, that formatting event was exactly what I was looking for. Would
this be considered a bug (i.e., will it likely be fixed in patches), or
is it an intended feature of some sort?

Regards,
Matt
 
B

Bart Mermuys

Hi,

Matt said:
Thanks, that formatting event was exactly what I was looking for. Would
this be considered a bug (i.e., will it likely be fixed in patches), or is
it an intended feature of some sort?

I like to believe it's a bug or at least a problem that needs to be fixed,
but i have no idea if there will be patches or anything like that.

Greetings
 
Y

Yuan Ren[MSFT]

Hi Matt,

Thanks for your reply!

I have received you demo and performed the current problem. It seems some
unexpected behavior was occurred for ComboBox control. So, I need more time
to debug the current project. Maybe I'll contact with Dev team for the
current issue, I sorry for inconvenience.

Thanks for your understanding!

Yuan Ren [MSFT]
Microsoft Online Support
 
Y

Yuan Ren[MSFT]

Hi Matt,

Thanks for your patience!

I have performed the issue. I'm not very clear about why you both set the
data binding of the ComboBox control and use "AddRange" method in it. So I
don't think this is a best practice for winform control's data binding.

Otherwise, if you need to do this, I suggest you call the "AddRange" method
likes below:
this.myCB.Items.AddRange(new object[] {
"",
"Mr",
"Ms",
"Mrs",
"Dr"});
I mean you add a null value for matching some related value from database.
After my performing, it seems the application runs well.

I appreciate your understanding and hope the above information helps, if
you have any issues or concerns please let me know. I will be happy to be
of further assistance.

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top