|
-
Aug 23rd, 2023, 02:53 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Combobox modification
I am looking for a means to take a combobox and bind to a column other than the one displayed. i.e., I do not want a multi column combobox, unless there is one out there that I would not have to spend a large amount purchasing. The combobox is bound to a table that has two columns with values that I need. One column has the name, the other the ID. I need to display the name, but use the undisplayed ID.
I have seen something about this before but have been unable to find it again.
Can anyone steer me to how I can either get or make the control do what I want?
-
Aug 23rd, 2023, 03:05 PM
#2
Re: Combobox modification
The datasource is the datatable. The selecteditem is going to be the row in the datatable, no matter what field you display, so you have everything that the table has. Therefore, I may be misunderstanding the question.
My usual boring signature: Nothing
 
-
Aug 23rd, 2023, 03:35 PM
#3
Thread Starter
Fanatic Member
Re: Combobox modification
OK, here is what I am after. I have a table with a set of columns, two of which I need. One of the columns is an ID (incremental integer starting with 1), and the other is a name. By the way, after thinking about it, I really am not interested in a multi-column combobox.
The combobox has the SelectedItem property set to the Name column. The value that I want is the ID from another column, which I do not want to display in the combobox. The combobox is populated as seen in the code below.
Would my problem be resolved simply by setting the properties for DisplayMember and SelectedItem? That would be a lot easier than my, not so great, idea of setting up a keypress event that runs a query to extract the ID from the table using value of the SelectedItem. I am really hoping for something better than that.
Code:
Public Sub GetOwnerComboBox()
MyError = "Manager query failed."
cboOwner.Items.Clear()
MasterBase.AddParam("@active", True)
MasterBase.MasterBaseQuery("SELECT colFullName FROM sitContacts WHERE colActive=@active")
If RecordCount > 0 Then
For Each r As DataRow In MasterBase.ListDataSet.Tables(0).Rows
cboOwner.Items.Add(r("colFullName"))
Next
End If
End Sub
-
Aug 23rd, 2023, 03:51 PM
#4
Re: Combobox modification
Adapted from https://stackoverflow.com/questions/...-combobox-item, but untested:
Code:
cboOwner.Items.Add(New DictionaryEntry(r("colFullName"), r("colID")) ' Your code doesn't seem to indicate the name of the ID column so I'm just guessing
To retrieve the ID:
Code:
CType(cboOwner.SelectedItem,DictionaryEntry).Key
Of course, you'll need to modify the existing SQL query to include the ID field so that it can be referenced using the code above.
Edit to add: If you were doing traditional database interactions + binding, then this would all be moot and you would just assign the DataSource, DisplayMember, and ValueMember properties of the ComboBox appropriately.
-
Aug 23rd, 2023, 04:29 PM
#5
Re: Combobox modification
Edit to add: If you were doing traditional database interactions + binding, then this would all be moot and you would just assign the DataSource, DisplayMember, and ValueMember properties of the ComboBox appropriately.
This seems like the answer. Then you could access the ID using the "SelectedValue" property. Why isn't the cbo datasource not set to MasterBase.ListDataSet.Tables(0)??
-
Aug 23rd, 2023, 04:37 PM
#6
Thread Starter
Fanatic Member
Re: Combobox modification
I am not quite sure how to make that work, but on the assumption I can figure it out, it seems to me that this would display both of the values in the combobox. This is not what I want to do.
What I am after is to take the combobox, already populated with the values from colFullName, Select one of the displayed names (SelectedItem) and then get the value from colContactID. This value is the one I actually want to use.
Currently, I am able to extract that value by running the routine below. However, I believe it would be a lot more efficient to just be able to grab that value at the same time I make a selection from combobox.
While the method does work, colFullName column is not unique.
Code:
Private Sub txtComplete_Validating(sender As Object, e As CancelEventArgs) Handles txtComplete.Validating
If (CInt(txtComplete.Text) < 0 Or CInt(txtComplete.Text) > 100) Then
MsgBox("Please enter an integer from 0 to 100.", vbCritical)
txtComplete.Text = ""
txtComplete.Focus()
End If
End Sub
-
Aug 23rd, 2023, 04:53 PM
#7
Re: Combobox modification
What I am after is to take the combobox, already populated with the values from colFullName, Select one of the displayed names (SelectedItem) and then get the value from colContactID. This value is the one I actually want to use.
That's exactly what valuemember does. Lots of examples of how valuemember and selectedvalue work.
-
Aug 23rd, 2023, 05:00 PM
#8
Re: Combobox modification
don't:
Code:
Public Sub GetOwnerComboBox()
MyError = "Manager query failed."
cboOwner.Items.Clear()
MasterBase.AddParam("@active", True)
MasterBase.MasterBaseQuery("SELECT colFullName FROM sitContacts WHERE colActive=@active")
If RecordCount > 0 Then
For Each r As DataRow In MasterBase.ListDataSet.Tables(0).Rows
cboOwner.Items.Add(r("colFullName"))
Next
End If
End Sub
Do:
Code:
Public Sub GetOwnerComboBox()
MasterBase.MasterBaseQuery("SELECT colFullName FROM sitContacts WHERE colActive=@active")
cboOwner.DisplayMember = "colFullName"
cboOner.ValueMember = "ID"
cdoOWner.Datasource = MasterBase.ListDataSet.Tables(0)
End Sub
By the way, this is where your use of a single consolidated data access class lie this is goign to start to fail you ....
Bottom line, set the ValueMember to the name of hte cl with the ID in it... set the DisplayMember to the name of the col you want to display, then set the dataSourcde to the datatable with the data in it.
-tg
-
Aug 23rd, 2023, 05:37 PM
#9
Re: Combobox modification
 Originally Posted by gwboolean
I am not quite sure how to make that work, but on the assumption I can figure it out, it seems to me that this would display both of the values in the combobox. This is not what I want to do.
What I am after is to take the combobox, already populated with the values from colFullName, Select one of the displayed names (SelectedItem) and then get the value from colContactID. This value is the one I actually want to use.
Currently, I am able to extract that value by running the routine below. However, I believe it would be a lot more efficient to just be able to grab that value at the same time I make a selection from combobox.
While the method does work, colFullName column is not unique.
Code:
Private Sub txtComplete_Validating(sender As Object, e As CancelEventArgs) Handles txtComplete.Validating
If (CInt(txtComplete.Text) < 0 Or CInt(txtComplete.Text) > 100) Then
MsgBox("Please enter an integer from 0 to 100.", vbCritical)
txtComplete.Text = ""
txtComplete.Focus()
End If
End Sub
Did you try the code I posted? Or am I just wasting my time?
-
Aug 23rd, 2023, 06:48 PM
#10
Thread Starter
Fanatic Member
Re: Combobox modification
Yes, I did, and several variations. I could not make that work. Not to say it can't, I just couldn't do it.
Bottom line, set the ValueMember
Nice. That looks familiar... then I noticed it uses a dataset. I got no problem with using a dataset, although I haven't for a awhile.
That's exactly what valuemember does.
True. Thanks.
-
Aug 23rd, 2023, 06:54 PM
#11
Re: [RESOLVED] Combobox modification
It's too bad you chose to not post the code you tried and said what issues you were having with it. Good luck with your future endeavors.
-
Aug 23rd, 2023, 08:23 PM
#12
Re: [RESOLVED] Combobox modification
Nice. That looks familiar... then I noticed it uses a dataset. I got no problem with using a dataset, although I haven't for a awhile.
Not sure why you say that. You program uses MasterBase.ListDataSet. Whether you use a datset datatable or a datatable that's not part of a dataset makes no difference.
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
|