Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

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.
0
Agent909
Asked:
Agent909
  • 4
  • 3
  • 3
  • +1
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
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

0
 
Meir RivkinFull stack Software EngineerCommented:
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

0
 
AshokCommented:
Try

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

HTH
Ashok
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AshokCommented:
Try

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

HTH
Ashok
0
 
Agent909Author Commented:
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.
0
 
AshokCommented:
I do not have VS 2008 working at this moment (have to re-install).  I will let you know in the evening.

Thanks,
Ashok
0
 
Agent909Author Commented:
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!
0
 
CodeCruiserCommented:
How about changing
cboDocType.DisplayMember = "Code"
cboDocType.ValueMember = "Code"

to
cboDocType.DisplayMember = "Code"
cboDocType.ValueMember = "Description"

Then in the SelectedIndexChanged event
labCode.Text = cbDocType.SelectedValue
0
 
Agent909Author Commented:
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?
0
 
CodeCruiserCommented:
It wont because that does not fire the selectedindexchanged event. I am not sure if autocomplete works with combobox.
0
 
Agent909Author Commented:
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.
0
 
CodeCruiserCommented:
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.
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

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