Getting VB.NET ComboBox to accept data like the ASP.NET DropDownList

  • Thread starter Thread starter Joe Schmoe
  • Start date Start date
J

Joe Schmoe

All I want to to be able to take a two-column DataReader (One column with
the Item ID number, the other with Item Description text) and load it into a
Windows Forms ComboBox (Set to DropDownList mode) so that I the dropdown
shows the Item Descriptions, but returns the Item ID number when selected.
Completely easy in ASP.NET, but I cannot figure out how to do the same in a
Windows Forms app. Obviously the Windows Forms ComboBox is a lot more
feature-filled, but all I want is a single-row item picker.

Code that does exactly what I want in ASP.NET follows. Please help me
translate it to what I need to do the same thing in a Windows Forms app.
Thanks!

Sub LoadRetailerGroups()

' Not using a sproc simply because this app is only a one-shot utility
to load data once

Dim dbQuery As String = "SELECT Distinct RetailerGroupID, RetailerGroup
FROM tbl_RetailerGroup ORDER BY RetailerGroup`"

Dim dbCmd As New System.Data.SqlClient.SqlCommand(dbQuery, dbCon)

dbCon.Open()

Dim dbReader As SqlDataReader

dbReader = dbCmd.ExecuteReader()

ddlRetailerGroup.DataSource = dbReader

ddlRetailerGroup.DataTextField = "RetailerGroup"

ddlRetailerGroup.DataValueField = "RetailerGroupID"

ddlRetailerGroup.DataBind()

dbReader.Close()


dbCon.Close()

End Sub



Thanks!
 
Hi,

Joe Schmoe said:
All I want to to be able to take a two-column DataReader (One column with
the Item ID number, the other with Item Description text) and load it into
a Windows Forms ComboBox (Set to DropDownList mode) so that I the dropdown
shows the Item Descriptions, but returns the Item ID number when selected.
Completely easy in ASP.NET, but I cannot figure out how to do the same in
a Windows Forms app. Obviously the Windows Forms ComboBox is a lot more
feature-filled, but all I want is a single-row item picker.

Code that does exactly what I want in ASP.NET follows. Please help me
translate it to what I need to do the same thing in a Windows Forms app.
Thanks!

Sub LoadRetailerGroups()

' Not using a sproc simply because this app is only a one-shot utility
to load data once

Dim dbQuery As String = "SELECT Distinct RetailerGroupID, RetailerGroup
FROM tbl_RetailerGroup ORDER BY RetailerGroup`"

Dim dbCmd As New System.Data.SqlClient.SqlCommand(dbQuery, dbCon)

dbCon.Open()

Dim dbReader As SqlDataReader

dbReader = dbCmd.ExecuteReader()

ddlRetailerGroup.DataSource = dbReader

ddlRetailerGroup.DataTextField = "RetailerGroup"

ddlRetailerGroup.DataValueField = "RetailerGroupID"

ddlRetailerGroup.DataBind()

dbReader.Close()


dbCon.Close()

End Sub

You can't bind winforms ComboBox to a DataReader instead use a
SqlDataAdapter and a DataTable (or DataSet), eg:

Dim dbQuery As String = "SELECT Distinct RetailerGroupID, RetailerGroup FROM
tbl_RetailerGroup ORDER BY RetailerGroup"
Dim RetailerGroupAdapt As New SqlDataAdapter( dbQuery, dbConn )
Dim RetailerGroupTable As New DataTable("RetailerGroup")

RetailerGroupAdapt.Fill( RetailerGroupTable )

RetailerGroupCbo.DataSource = RetailerGroupTable
RetailerGroupCbo.DisplayMember = "RetailerGroup"
RetailerGroupCbo.ValueMember = "RetailerGroupID"

RetailerGroupCbo.SelectedValue should give you the ID of the selected row.

hth,
Greetings
 
Thanks!

One more question now: When I try to access cbRetailerGroup.SelectedValue, I
am getting "Cast from type 'DataRowView' to type 'Integer' is not valid."
Any ideas? Code below.

Private Sub cbRetailerGroup_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cbRetailerGroup.SelectedIndexChanged

Dim RetailerGroupID As Integer

RetailerGroupID = cbRetailerGroup.SelectedValue

' Test to make sure getting correct value
Label3.Text = RetailerGroupID

End Sub
 
Ok, here is the deal. The SelectedValue property is not valid in this
context b/c you bound to a data row. So, you have to grab the object that
was bound (actually a DataRowView object), get its DataRow, then grab your
column out of that row. I am sticking with ASP.NET programming from now on,
working with Windows Forms apps is a bunch of overcomplicated shitwork.



Private Sub cbRetailerGroup_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cbRetailerGroup.SelectedIndexChanged



Dim RetailerGroupId As Integer

Dim DRV As System.Data.DataRowView =
CType(Me.ComboBox1.SelectedItem, System.Data.DataRowView)

RetailerGroupId = DRV.Item("RetailerGroupID") 'assumes that
"RetailerGroupID" is the column name



End Sub
 
Hi,

Joe Schmoe said:
Ok, here is the deal. The SelectedValue property is not valid in this
context b/c you bound to a data row. So, you have to grab the object that
was bound (actually a DataRowView object), get its DataRow, then grab your

You don't need to do it that way, whatever SelectedValue returns depends on
the ValueMember property, so make sure you set it correctly. It's also
possible SelectedIndexChanged fires before ValueMember is set, so some
checks are needed:

RetailerGroupCbo.DataSource = RetailerGroupTable
RetailerGroupCbo.DisplayMember = "RetailerGroup"
RetailerGroupCbo.ValueMember = "RetailerGroupID"

Private Sub cbRetailerGroup_SelectedIndexChanged(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles
cbRetailerGroup.SelectedIndexChanged

If ( (cbRetailerGroup.SelectedIndex <> -1) And _
(cbRetailerGroup.ValueMember <> "") ) Then

RetailerGroupId = cbRetailerGroup.SelectedValue
End If

End Sub

hth,
greetings
 
Back
Top