Combobox Binding to ArrayList for Lookup

stevecking

Active member
Joined
Dec 27, 2006
Messages
32
Programming Experience
5-10
I'm confused by what should be a relatively straight forward design. It uses an ArrayList with classes added which is the DataSource for a combobox. The DisplayMember is then "LastFirst" and the ValueMember is the ID of the name. Sometimes the dropdown on the combobox is empty, sometimes the combobox seems to display the "LastFirst" correctly but after selecting it the text disappears. I've been quite unsuccessful figuring this rather simple seeming issue out. Any help would be appreciated.

Public Class LookupName

Private _ID As Integer
Private _FirstName As String
Private _LastName As String

Public Sub New(ByVal ID As Integer, ByVal FirstName As String, ByVal LastName As String)
_ID = ID
_FirstName = FirstName
_LastName = LastName
End Sub
Public ReadOnly Property ID() As Integer
Get
ID = _ID
End Get
End Property
Public ReadOnly Property FirstName() As String
Get
FirstName = _FirstName
End Get
End Property
Public ReadOnly Property LastName() As String
Get
LastName = _LastName
End Get
End Property
Public ReadOnly Property LastFirst() As String
Get
Return _LastName & ", " & _FirstName
End Get
End Property
Public Overrides Function ToString() As String
Return _LastName & ", " & _FirstName
End Function
End Class

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

Me.TblDataTableAdapter.Fill(Me._MyDatabase_1DataSet.tblData)
Using Cnn As New SqlServerCe.SqlCeConnection(My.Settings.dbConnectionString.ToString)
Dim cmd As New SqlServerCe.SqlCeCommand("SELECT ID, First, Last FROM Names", Cnn)
cmd.CommandType = CommandType.Text
Cnn.Open()

Dim dr As SqlServerCe.SqlCeDataReader = cmd.ExecuteReader
While dr.Read
comboArray.Add(New LookupName(dr("ID"), dr("First"), dr("Last")))
End While
Me.NameIDComboBox.DataSource = comboArray

With Me.NameIDComboBox
.DataSource = comboArray
.DisplayMember = "LastFirst"
.ValueMember = "ID"
.DataBindings.Add("SelectedValue", TblDataBindingSource, "NameID")
End With

End Using

End Sub
 
Please use code tags when posting code

I always found it easier to bind Combos to datatables, not arraylists.. And I usually make a typed datatable called "DispVal" and have 2 columns, "Disp" and "Val"

Then the code to bind make it show:

VB.NET:
Dim dt as New MyDataSet.DispVal()
myCombo.DataSource = dt
myCOmbo.DisplayMember = "Disp"
myCombo.ValueMember = "Val"

dt.AddDispValRow("Displayed Item", "Actual Value")


I dont usually do binding in code, so I dont have the exact syntax to hand for binding the selectedvalue to whatever table is to be looked up. Perhaps someone else can help
 
Last edited:
It would be simple for me to bind it if I didn't also want the LastName and FirstName to be displayed as LastFirst (King, Steve), which I do by building that into a class property. I know there are other ways of accomplishing what I'm trying to accomplish with an ArrayList but I've invested some time and sure would like to know how to do it. It's been floating around in my head for a week and it's like a puzzle that I can't finish.

You've posted a short snippet that I can infer some design concepts. The MyDataSet is a DataSet that you've created to contain a table with at least two columns; one to hold the value to be diplayed and one to hold the 'key' value that gets bound to the SelectedValue. The AddDispValRow probably performs a row INSERT into the table and you would invoke it like I did in a while loop to fill it? Let me know if I'm off base.
 
You've posted a short snippet that I can infer some design concepts. The MyDataSet is a DataSet that you've created to contain a table with at least two columns; one to hold the value to be diplayed and one to hold the 'key' value that gets bound to the SelectedValue. The AddDispValRow probably performs a row INSERT into the table and you would invoke it like I did in a while loop to fill it? Let me know if I'm off base.

Minor correction about client side vs server side data:

DataTable is client side
Add{0}Row is a method generated on the fly by the designer, where {0} is the name of the datatable, and it adds a new typed DataRow to the table:

Functionally equivalent:
VB.NET:
Dim dr as MyDataSet.DispValRow = myDataSetInstance.DispVal.NewDispValRow()
dr.Disp = "Display me"
dr.Val = "The value"
myDataSetInstance.DispVal.Rows.Add(dr)
VB.NET:
myDataSetInstance.DispVal.AddDispValRow("Display me", "The value")
Nothing is written to db. Here, only addition to client side


If data is to come from a database, I will normally write a query into the DispVal TableAdapter like:

SELECT LastName + ', ' + FIrstName as Disp, userId as Val FROM tblUsers

The DispVal TableAdapter might have tens or hundreds of these queries. Then:


myCombo.DataSource = myDispValTableAdapter.GetFirstNameLastNameData()


...


Dont forget also that a DataTable can have a column that is an EXPRESSION of other columns, and this can be shown in a label or combo
 
Back
Top