• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

DataBound ComboBox can not set SelectedValue to ID

I know this question comes up a lot, but I have only found workarounds and no answer.

I have a databound ComboBox that is bound to a class. The class is the wrapper for a lookup table (lets call them cLookups and cLookup).

The initialisation code looks a little like the following:

cbo.DisplayMember = "Description"
cbo.ValueMember = "ID"
cbo.DataSource = theLookups

I then want to set the currently selected item in the combobox to an ID in the lookup. So:

cbo.SelectedValue = "X"

This returns "Object reference not set to an instance of an object" as SelectedValue seems to be expecting an Object of type cLookup.

I could create a cLookup object and pass to SelectedItem, but this seems a little OTT.

I don't want to use a Find function on the combobox as this would mean having to know the description.

MS help states that as long as the type is the same as the type of ValueMember then it should work; these are both Strings.
0
dbasplus
Asked:
dbasplus
  • 5
  • 3
1 Solution
 
Robert SchuttSoftware EngineerCommented:
I tried this out with a simplified version, so not sure if this applies to your code, but HTH:
Public Class Form1

    Dim theLookups As New List(Of cLookup)

    Dim _busy As Boolean = False

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

        theLookups.Add(New cLookup(1, "item 1"))
        theLookups.Add(New cLookup(2, "item 2"))
        theLookups.Add(New cLookup(3, "item 3"))

        cbo.DisplayMember = "Description"
        cbo.ValueMember = "ID"
        cbo.DataSource = theLookups

    End Sub

    Private Sub cbo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbo.SelectedIndexChanged
        If Not _busy Then
            _busy = True
            TextBox1.Text = cbo.SelectedValue
            _busy = False
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        If Not _busy Then
            _busy = True
            cbo.SelectedValue = Integer.Parse(TextBox1.Text)
            _busy = False
        End If
    End Sub
End Class

Public Class cLookup
    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 _desc As String
    Public Property Description() As String
        Get
            Return _desc
        End Get
        Set(ByVal value As String)
            _desc = value
        End Set
    End Property

    Sub New(id As Integer, desc As String)
        _id = id
        _desc = desc
    End Sub

End Class

Public Class cLookups

End Class

Open in new window

Note line 30: the issue is that you need to assign the right type of data, it doesn't work if you assign a string. But this example works in the sense that the cbo selection is changed correctly. Note that you have to select the value in the textbox and replace it (as opposed to using backspace) due to simplicity of coding, add a try/catch later.
0
 
Robert SchuttSoftware EngineerCommented:
I realize now that this was not what you wanted to hear, probably the reason all stayed quiet... Maybe you can use a Linq predicate instead of a loop to find the correct object?

EDIT: or maybe I'm wrong and this is actually a simpler way of doing what you need, I can only hope.
0
 
Robert SchuttSoftware EngineerCommented:
I'm rambling again, sorry about that but do have a good look please at the difference between SelectedItem and SelectedValue because SelectedItem would expect a cLookup but SelectedValue I believe needs to be the same type as the member it refers to (ID in your case).
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
dbasplusAuthor Commented:
Thank you for your efforts Robert.
Here's the strange thing. I have created a test app to test your example and it works great. I then went on and inserted a whole heap of code from my main app for the clookup and clookups classes and the code still works fine.
I just can't work out why in my main app the SelectedValue property is expecting and object of type cLookup and not a string!
0
 
dbasplusAuthor Commented:
Found it.
I had the ID rather lazily defined as:
    Public ID As String

Open in new window

I needed to define it the correct way, as done by Robert:
    Private _id As String
    Public Property ID() As String
        Get
            Return _id
        End Get
        Set(ByVal value As String)
            _id = value
        End Set
    End Property

Open in new window

0
 
Robert SchuttSoftware EngineerCommented:
Well it's a load off my mind at least that I didn't post a load of bs after all. ;-)

Could it have anything to do with the order of setting ValueMember and DataSource maybe?

Can you post more code or give some insight in the structure of your application to get to the bottom of this?
0
 
Robert SchuttSoftware EngineerCommented:
Oops, I see you solved it while I was typing... Nice!
0
 
dbasplusAuthor Commented:
No worries. It just shows that it doesn't help to cut corners.
Thanks for you help.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now