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 6 4759
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 -- v-****@online.microsoft.com (remove .online).
I'm looking forward your mail!
Regards,
Yuan Ren [MSFT]
Microsoft Online Support
Hi,
"Matt" <br**********@community.nospam> wrote in message
news:OM*************@TK2MSFTNGP12.phx.gbl... 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 Thanks, 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
Bart Mermuys wrote: Hi,
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
Thanks, Matt
Hi,
"Matt" <br**********@community.nospam> wrote in message
news:eG*************@tk2msftngp13.phx.gbl... 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 Regards, Matt
Bart Mermuys wrote: Hi,
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
Thanks, Matt
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
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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
7 posts
views
Thread by Nicolae Fieraru |
last post: by
|
8 posts
views
Thread by Zlatko Matić |
last post: by
|
7 posts
views
Thread by charliewest |
last post: by
|
2 posts
views
Thread by Agnes |
last post: by
|
reply
views
Thread by dbuchanan |
last post: by
|
4 posts
views
Thread by jon f kaminsky |
last post: by
|
5 posts
views
Thread by Steve B. |
last post: by
|
2 posts
views
Thread by shumaker |
last post: by
| | | | | | | | | | | |