Link to home
Start Free TrialLog in
Avatar of Agent909
Agent909

asked on

How do I access another column of data in a ComboBox?

Merry Christmas, experts.  I hope someone is out there.

Here's my situation:
I have a Codes table with two fields, "Code" and "Description".  I have successfully added the codes to the ComboBox, cboDocType, and want to access the "Description" column when the user selects a code in the combobox.  The "Description" needs to go into a label control, "labCode", so that the user will know that they selected the correct code.  What code do I need to accomplish this?  Here's my code so far:

Dim dsCodes As New DataSet
Dim codesAdapter As New SqlDataAdapter("SELECT * FROM Codes ORDER BY Code", myConnection)
codesAdapter.Fill(dsCodes, "Codes")
cboDocType.DataSource = dsCodes.Tables("Codes")
cboDocType.DisplayMember = "Code"
cboDocType.ValueMember = "Code"
labCode.Text = "I want to display the Codes.Description here."

Thank you.
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

here:
Dim dt As DataTable = dsCodes.Tables("Codes")
        Dim dr As DataRow = dt.Rows.Cast(Of DataRow)().Where(Function(n) n("Description").ToString() = cboDoctType.Text).FirstOrDefault()
        labCode.Text = dr("Description").ToString()

Open in new window

i made a mistake, here's the fix:
Dim dt As DataTable = dsCodes.Tables("Codes") 
        Dim dr As DataRow = dt.Rows.Cast(Of DataRow)().Where(Function(n) n("Code").ToString() = cboDoctType.Text).FirstOrDefault() 
        labCode.Text = dr("Description").ToString()

Open in new window

Try

Convert.ToString(ComboBox1.SelectedRow("Description"))

HTH
Ashok
Try

labCode.Text = Convert.ToString(ComboBox1.SelectedRow("Description"))

HTH
Ashok
Avatar of Agent909
Agent909

ASKER

Sedqwick:  Your solution worked, but only for the first code in the list.  I need it to display the description when the value in the ComboBox changes.  Your solution looks brilliant!  I can't imagine figuring that one out!  I hope you can help me complete the answer to my problem.  Thanks!
PS:  I did try putting the code into cboDocType_SelectedIndexChanged, but that didn't work.  I got errors.

Ashok111:  I couldn't get your solution to work.
I do not have VS 2008 working at this moment (have to re-install).  I will let you know in the evening.

Thanks,
Ashok
Ashok:  I put your code in cboDocType_SelectedIndexChanged, and the error is:
'SelectedRow' is not a member of 'System.Windows.Forms.ComboBox'
I got the same error when I put your code in Form_Load.

I'll be looking for your solution this evening.  Thank you!
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CodeCruiser:  This worked after I added '.ToString' at the end of 'SelectedValue".  It works when I click on the down arrow and select a code, but when I am in data entry mode, and type a code, such as "C" for "Civil", the value of labCode doesn't change.  Any ideas?
It wont because that does not fire the selectedindexchanged event. I am not sure if autocomplete works with combobox.
CodeCruiser:  ComboBox does have an AutoCompleteMode.  I set this to "Append", and it displayed the correct description when I left the cboDocType field.  It would be nice if it would display the description as soon as I enter the code.  Maybe this is asking too much.  I have used Infragistics ComboBox before, and it will do what I want.  It's so expensive now, though.  About $800 a year.

Thank you for your help.  I will award you the points, but would appreciate any further comments you might have.
Glad to help. Most commercial controls (i use DevExpress) provide a lot of additional functionality in combobox which is very difficult to achieve with the builtin combobox.