473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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!
Nov 21 '05 #1
4 8488
Hi,

"Joe Schmoe" <Jo********@hamstervision.com> wrote in message
news:MjA2f.8664$417.3736@okepread02...
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!

Nov 21 '05 #2
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

"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:O4**************@tk2msftngp13.phx.gbl...
Hi,

"Joe Schmoe" <Jo********@hamstervision.com> wrote in message
news:MjA2f.8664$417.3736@okepread02...
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!


Nov 21 '05 #3
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

"Joe Schmoe" <Jo********@hamstervision.com> wrote in message
news:kZP2f.8678$417.907@okepread02...
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

"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:O4**************@tk2msftngp13.phx.gbl...
Hi,

"Joe Schmoe" <Jo********@hamstervision.com> wrote in message
news:MjA2f.8664$417.3736@okepread02...
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!



Nov 21 '05 #4
Hi,

"Joe Schmoe" <Jo********@hamstervision.com> wrote in message
news:kfQ2f.8679$417.5729@okepread02...
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

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

"Joe Schmoe" <Jo********@hamstervision.com> wrote in message
news:kZP2f.8678$417.907@okepread02...
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

"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:O4**************@tk2msftngp13.phx.gbl...
Hi,

"Joe Schmoe" <Jo********@hamstervision.com> wrote in message
news:MjA2f.8664$417.3736@okepread02...
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!



Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: George Hartas | last post by:
I am using Visual C# .NET 2003 to make a ComboBox accept both mouse and keyboard selection. For mouse selection code, I double-clicked ComboBox to get the default "comboBox1_SelectedIndexChanged"...
1
by: anonymous | last post by:
I've been trying to put a them, please help me out. Here's the major parts of my code: public Form1() { DataSet myDataSet = new DataSet("myDataSet"); DataTable testTable = new...
0
by: Doug | last post by:
This is a repost of an item that I still cannot resolve. I have 3 combo boxes. The first leads to the second to the third. When I have selected a value in the second box, the third box shows...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
6
by: Matt | last post by:
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...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
9
by: Jakob Lithner | last post by:
1) I have a DataGridView with edit capability. But in some columns I want to limit the input with a DropDownList. There is no inbuilt column for DropDownLists so I intended to add one myself. I...
4
by: Jerad Rose | last post by:
I'm baffled by this -- is there not a typed object used for ComboBox Items? Best I can tell, all of the methods for ComboBox that accept an Item are of type Object. Why in the world is a...
2
by: Redivivus | last post by:
Hi How can i disable Paste on ComboBox? ComboBox DropDownStyle is DropDown. ComboBox must accept only numbers. I know how to handle keypress event to accept only digits, but how to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.