-
Oct 28th, 2021, 10:33 AM
#1
Thread Starter
Fanatic Member
Combobox Tricks
I would like to be able to get data from two columns of a combobox. I have found a lot of instruction for displaying multiple columns, but I really would only want to display one column and be able to insert data from more than one column.
I have come up with a couple of ways to get the data I want, outside of using a combobox, but that defeats the purpose of what I would like to do.
Can anyone steer me to some content that might explain how this could be done?
-
Oct 28th, 2021, 10:58 AM
#2
Re: Combobox Tricks
If you bind your ComboBox to a datasource, you have the displaymember which is what you see in the. ComboBox, and the valuemember, which is an associated value.
Alternatively, you can use a class as a custom listitem, override the ToString to return the property you want displayed in the combobox, and the other properties can be accessed in the selectedindexchanged event by casting selecteditem to your custom class…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 28th, 2021, 10:59 AM
#3
Re: Combobox Tricks
There's several ways but it depends on your data and how you are loading the combo box - and how much effort you want to put into it.
Use a datatable, make sure it has an ID of some kind on it. Bbind the datatable to the combobox: Set the .DisplayMember to the field name you want to display. Set the .ValueMember to the ID, then set the .DataSource of the combobox to the DataTable. Then in the selectedIndexchanged event, get the .Value of the combobox... that will give you the ID of the selected row, from there you can get the rest of the data you need.
That's probably the simplest way, and the easiest if your data is coming from a database.
Another way involves creating a class to hold your data and creating a List(Of) to hold it.
-tg
-
Oct 28th, 2021, 11:04 AM
#4
Re: Combobox Tricks
Originally Posted by techgnome“
…Then in the selectedIndexchanged event, get the .Value of the combobox..
You mean the SelectedValue?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 28th, 2021, 11:56 AM
#5
Thread Starter
Fanatic Member
Re: Combobox Tricks
I have not yet attempted to work the suggestions, but I hope to be a little more clear about what I am after. My combobox, as it is currently set, displays one column (DisplayMember), which is also set as the ValueMember.
This one column provides one data from the record. Additionally, I want to obtain the data from an second column from the same record. I do not need, or want, the additional column to display in the combobox. Basically what I believe I need is to have two ValueMembers, if that is possible.
Now I will review the suggestions so far.
how much effort you want to put into it?
I would prefer no effort at all, but am willing to put some effort into it.
-
Oct 28th, 2021, 12:44 PM
#6
Re: Combobox Tricks
As explained before, you have the ability to retrieve two values. The displaymember value and the SelectedValue value.
Take a look,
Code:
Me.ComboBox1.DisplayMember = "groupid"
Me.ComboBox1.ValueMember = "crop"
Me.ComboBox1.SelectedValue = "crop"
Me.ComboBox1.DataSource = Me.WaterDataSet.groups
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(Me.ComboBox1.Text)
MessageBox.Show(Me.ComboBox1.SelectedValue.ToString)
End Sub
-
Oct 28th, 2021, 03:53 PM
#7
Thread Starter
Fanatic Member
Re: Combobox Tricks
As explained before, you have the ability to retrieve two values.
OK, I can see how that works (although I am unable to see any previous explanation by you), but after playing with my own solutions (really crappy but working solution), my specifications have changed. There are 3 fields I need to deal with. FullName, EmployeeID, JobTitle from Table1. I would like to display FullName and jobTitle in the combobox, but I need to update two fields in table2, Manager, ManagerID.
Now it looks to me like your method would still work, however, I would still like to be able to display two columns instead of just the one.
-
Oct 28th, 2021, 04:00 PM
#8
Re: Combobox Tricks
Look into concatenating the two fields (FullName and JobTitle) in your SQL query
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 28th, 2021, 04:14 PM
#9
Re: Combobox Tricks
OK, I can see how that works (although I am unable to see any previous explanation by you),
Of course you can't, that was my first post in this thread. lol
But .paul and techgnome both mentioned the two different values.
Last edited by wes4dbt; Oct 28th, 2021 at 08:18 PM.
-
Oct 28th, 2021, 04:54 PM
#10
Re: Combobox Tricks
Code:
SELECT EmployeeID, FullName, JobTitle, CONCAT(FullName, ' ', JobTitle) AS DisplayField FROM Table1
Then you can use DisplayField as your DisplayMember
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 28th, 2021, 05:13 PM
#11
Re: Combobox Tricks
If you bind your ComboBox to a DataSource, you can set any Field as DisplayMember and not bother with a ValueMember, just cast your SelectedItem like this...
Code:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim row As DataRowView = DirectCast(ComboBox1.SelectedItem, DataRowView)
MsgBox(row.Item(columnIndex))
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 31st, 2021, 07:57 AM
#12
Re: Combobox Tricks
My preference is what techgnome mentioned in regards to a List(Of T).
Here I load data using the following class.
Screenshot Attachment 182847
Code:
Public Class Employee
Public Property EmployeeID As Integer
Public Property TitleOfCourtesy As String
Public Property FirstName As String
Public Property LastName As String
Public Property Fullname As String
Public Property BirthDate As Date
Public Property HomePhone As String
Public Overrides Function ToString() As String
Return Fullname
End Function
End Class
Read the data from where ever e.g. database, text file, json file etc.
Code:
Public Class Form1
Private ReadOnly EmployeeBindingSource As BindingSource = New BindingSource()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
EmployeeBindingSource.DataSource = Operations.ReadEmployee()
EmployeeComboBox.DataSource = EmployeeBindingSource
End Sub
Private Sub CurrentButton_Click(sender As Object, ea As EventArgs) Handles CurrentButton.Click
Dim e = CType(EmployeeBindingSource.Current, Employee)
MessageBox.Show($"{e.TitleOfCourtesy} {e.LastName}{vbCr}{e.BirthDate.ToShortDateString()}{vbCr}{e.HomePhone}")
End Sub
End Class
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|