Problem with Nullable( Of Integer) bound to combobox

  • Thread starter Flomo Togba Kwele
  • Start date
F

Flomo Togba Kwele

I struggled with a problem I had with a combobox bound to a
bindingsource which has as its datasource a collection of custom
objects. The comobobx was bound to a nullable integer property of the
custom object.

When I tried to change the value of the dropdown and tabbed off the
control, the dropdown index would revert back to what it originally
was, so I could not change the value. I placed a breakpoint at the
Leave event of the combobox and single-stepped. It went into the
getter, not the setter!

So I changed the definition of the property in the object bound to the
combobox from Nullable(Of Integer) to Integer and it worked properly.
After the Leave event, it goes into the Setter.

Am I doing something wrong?
 
L

Linda Liu [MSFT]

Hi Flomo,

I performed a test based on your description and did see the same thing as
you did.

We could change the value of type Nullable(of Integer) by assigning a value
of type Integer or Nullable(of Integer) to it directly. It seems that the
problem comes from data binding, which in this case doesn't push the new
value to the data source.

I subscribe and handle the Parse event of the Binding object that is added
to the DataBindings property of the ComboBox control, and find that the new
value being pushed to the data source is of type Integer. The following is
the code to subscribe the Parse event of the Binding object and the code of
the event handler.

AddHandler Me.ComboBox1.DataBindings.Item(0).Parse, AddressOf comboboxParse

Private Sub comboboxParse(ByVal sender As Object, ByVal e As
System.Windows.Forms.ConvertEventArgs)
' e.Value is of type Integer.
Dim obj As Object = e.Value
End Sub

In the Parse event handler, e.Value is of type Integer. Apparently, data
binding couldn't update the property of type Nullable(Of Integer) with the
value of Integer.

The workaround is simple, i.e. changing the property value by assigning the
new value to it directly in the Parse event handler. The following is a
sample.

Private Sub comboboxParse(ByVal sender As Object, ByVal e As
System.Windows.Forms.ConvertEventArgs)
' the obj is a variable refereced to the current object in the data
source
obj = e.Value
End Sub

I have performed a test on the above workaround and confirmed it works.

Hope this helps.

If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
F

Flomo Togba Kwele

Linda,

Thanks for the reply.

In it you said:
Private Sub comboboxParse(ByVal sender As Object, ByVal e As
System.Windows.Forms.ConvertEventArgs)
' the obj is a variable refereced to the current object in the data
source
obj = e.Value
End Sub

I assumed that by obj you meant sender. If the assumption is correct,
it does not fix the problem.

My code is:

Dim bndTypeOffset As Binding =
cboTypeOffset.DataBindings.Add("SelectedValue", CampaignBindingSource,
"CampaignDetail.TypeOffset")
AddHandler bndTypeOffset.Parse, AddressOf ParseTypeOffset

....
Private Sub ParseTypeOffset(ByVal sender As Object, ByVal e As
ConvertEventArgs)
sender = e.Value
End Sub
 
L

Linda Liu [MSFT]

Hi Flomo,

Thank you for your prompt response.

The 'obj' in the Parse event handler in my sample code refers to the
current object the data source to which the combobox is bound. In your
scenario, the data source is CampaignBindingSource.

You could get the current object the CampaignBindingSource through its
Current property. Convert the current object to a proper type and then set
'e.Value' to the current object's property which is of type Nullable(Of
Integer).

The following is a sample.

Private Sub ParseTypeOffset(ByVal sender As Object, ByVal e As
ConvertEventArgs)

CType(CampaignBindingSource.Current, YourObjectType).PropertyName = e.Value

End Sub

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
F

Flomo Togba Kwele

Thanks for the specifics, Linda.

Is it possible for you to send your entire test code to me? It would
be a great help.

Thanks, Flomo
 
L

Linda Liu [MSFT]

Hi Flomo,

The following is the complete code of my test. It requires a ComboBox
control to be added on the form.

Public Class Form1

Dim myBindingSource As BindingSource

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' set up the lookup list
Dim lookupList As New List(Of Lookup)
Dim obj As New Lookup
obj.ID = 1
obj.Name = "aaaa"
lookupList.Add(obj)

obj = New Lookup
obj.ID = 2
obj.Name = "bbbb"
lookupList.Add(obj)

obj = New Lookup
obj.ID = 3
obj.Name = "cccc"
lookupList.Add(obj)

Me.ComboBox1.DataSource = lookupList
Me.ComboBox1.DisplayMember = "Name"
Me.ComboBox1.ValueMember = "ID"

' set up the business list
Dim businessList As New List(Of Business)
Dim obj1 As New Business
obj1.CategoryID = 1
businessList.Add(obj1)

obj1 = New Business
obj1.CategoryID = 2
businessList.Add(obj1)

' set up the BindingSource
myBindingSource = New BindingSource
myBindingSource.DataSource = businessList

' bind the BindingSource to the ComoboBox control
Me.ComboBox1.DataBindings.Add("SelectedValue", myBindingSource,
"CategoryID")
' subscribe the Parse event of the Binding object
AddHandler Me.ComboBox1.DataBindings.Item(0).Parse, AddressOf
comboboxParse

End Sub

Private Sub comboboxParse(ByVal sender As Object, ByVal e As
System.Windows.Forms.ConvertEventArgs)
Dim currentobj As Object = myBindingSource.Current
CType(currentobj, Business).CategoryID = e.Value
End Sub

End Class

Public Class Lookup
Private _id As Integer
Public Property ID() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property

Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class

Class Business
Private _categoryId As Nullable(Of Integer)
Public Property CategoryID() As Nullable(Of Integer)
Get
Return _categoryId
End Get
Set(ByVal value As Nullable(Of Integer))
_categoryId = value
End Set
End Property
End Class

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
F

Flomo Togba Kwele

Linda,

Thanks for the post.

OK, I see it works. Now to complicate things with real-life issues.

My bindingsource's datasource is a custom collection of custom
objects, which itself contains a custom collection of custom objects -
parent/child.

In the Parse event, I can get to the child collection, but not to the
individual child responsible for raising the event.

As I showed before:

Dim bndTypeOffset As Binding =
cboTypeOffset.DataBindings.Add("SelectedValue", CampaignBindingSource,
"CampaignDetail.TypeOffset")
AddHandler bndTypeOffset.Parse, AddressOf ParseTypeOffset

CampaignDetail is the child set, TypeOffset is the property.

I cannot find a way to get to this property in the Parse event,
because the parent exposes the children only as a set, not as
individuals.
 
L

Linda Liu [MSFT]

Hi Flomo,

You may try the following code to get the current individual child object
in the Parse event handler:

Dim currentobj As Object = Me.BindingContext.Item(CampaignBindingSource,
"CampaignDetail").Current

If the problem is still not solved, you may send me a sample project that
could just reproduce the problem. To get my actual email address, remove
'online' from my displayed email address.


Sincerely,
Linda Liu
Microsoft Online Community 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