-
Aug 28th, 2023, 03:43 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Combobox routine failure
I recently replaced the first routine below that I was using for a combobox, with the second routine below. The first one worked fine for the combobox, but did not meet my need to be able to display one value of the record, while using another value of the record, which caused me to change to the second routine. I.e., I wanted the colFullName value to be displayed in the combobox, but when I made a selection I wanted the valued from colContactID.
When the code is executed, the column names are expressed for the values of both cboOwner.DisplayMember, and cboOwner.ValueMember. e.g., "colFullName", and "colContactID"
Since this is a new approach for me, I have been unable to figure what I might have done to cause this issue.
Code:
'cboOwner.Items.Clear()
MasterBase.AddParam("@active", True)
'MasterBase.MasterBaseQuery("SELECT colFullName,colContactID FROMsitContacts 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
Code:
Public Sub GetOwnerComboBox()
cboOwner.Items.Clear()
cboOwner.Text = ""
MasterBase.AddParam("@active", True)
MasterBase.MasterBaseQuery("SELECTcolFullName,colContactID FROM sitContacts WHERE colActive=@active")
cboOwner.DisplayMember ="colFullName"
cboOwner.ValueMember ="colContactID"
cboOwner.DataSource =MasterBase.ListDataSet.Tables(0)
cboOwner.Text = ContactName
End Sub
-
Aug 28th, 2023, 04:09 PM
#2
Re: Combobox routine failure
When the code is executed, the column names are expressed for the values of both cboOwner.DisplayMember, and cboOwner.ValueMember. e.g., "colFullName", and "colContactID"
How are you coming to this conclusion? Where do you see those values.
Code:
cboOwner.Text = ContactName
This doesn't look right. Been a while but I think with a databound combobox, if you want to default to a specific item in the collection you need to set the selectedindex to that item. I could be wrong on that.
-
Aug 28th, 2023, 04:14 PM
#3
Re: Combobox routine failure
SelectedItem is the displayed item
SelectedValue is the hidden id that you set
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 28th, 2023, 05:15 PM
#4
Thread Starter
Fanatic Member
Re: Combobox routine failure
How are you coming to this conclusion? Where do you see those values.
I see this in two places. The first is that is what is displayed on the form as cbo.Owner.Text. Additionally, I can see this as I step through the code.
Everything I can detect suggests to me that cboOwner.DisplayMember is expressed as the string "colFullName", and that cboOwner.ValueMember is expressed as the string "colContactID". However, I have been frequently wrong in my assessments.
That is true. I missed that as a leftover from something else I tried. Having said that, I took that out and nothing changes.
SelectedItem is the displayed item
SelectedValue is the hidden id that you set
I assumed that I should Exchange those values in the code to be as seen below
cboOwner.SelectedItem = "colFullName"
cboOwner.SelectedValue = "colContactID"
That change got me to a little bit different result. The combox displayed a list repating, "System.Data.DataR...", I was unable to read the rest of the line. I would guess that the combobox list is the same length as the dataset from the query.
I have seen this before, quite some time ago, and do not recall the significantce.
-
Aug 28th, 2023, 05:33 PM
#5
Re: Combobox routine failure
No, you had it right the first time. You should be setting the .DisplayMember and .ValueMember properties just as you were initially.
I'm fairly certain that the issue is arising from what you are assigning to the .DataSource property. Given your "MasterBase" database interaction approach, I'm not sure what the correct object is that you would assign to .DataSource, but what you are assigning to it in the code in your first post just doesn't seem right to me.
Edit to add:
...Or maybe it is correct. Note that the code in your opening post is missing a space after SELECT. I'm assuming that is just a typo in your posted code. If that space is actually missing in your production code, then your query can't be working at all in the first place.
-
Aug 28th, 2023, 05:59 PM
#6
Thread Starter
Fanatic Member
Re: Combobox routine failure
No, you had it right the first time.
I just found that out and have already changed it back. I do have some more information.
I found that the combobox does indeed have the list of contact names (colFullName).
1. The combobox is displaying, "colFullName" as cboOwner.txt. Having said that, I could not find where cboOwner.Text was assigned a value when the controls were bound. However, whatever I selected from the combobox list was saved (if I save it) as the string "colFullName".
-
Aug 28th, 2023, 06:03 PM
#7
Re: Combobox routine failure
 Originally Posted by gwboolean
I just found that out and have already changed it back. I do have some more information.
I found that the combobox does indeed have the list of contact names (colFullName).
1. The combobox is displaying, "colFullName" as cboOwner.txt. Having said that, I could not find where cboOwner.Text was assigned a value when the controls were bound. However, whatever I selected from the combobox list was saved (if I save it) as the string "colFullName".
I'm not following what you are describing. Are you able to post a screenshot of the combobox while the dropdown list is displayed?
-
Aug 28th, 2023, 08:18 PM
#8
Re: Combobox routine failure
I'm curious if the combobox is also bound? Not talking about the item list.
-
Aug 28th, 2023, 08:19 PM
#9
Thread Starter
Fanatic Member
Re: Combobox routine failure
Are you able to post a screenshot
Yeah, that wasn't too clear to me either, and I wrote it. Probably because I am not easily getting what is going on. So what you can see is the string colFullName where the value for cboOwner.txt should display. Everything below that is the list that meets the criteria and I would say conforms to cboOwner.DisplayMember.
I can see a couple of possibilities as to why the colFullName might be there, but nothing that I can clearly describe. But what will occur, should I select any of the names on the list, is that the value displayed as colFullName will be overwritten by the selected name from the list. If I save this change, the value saved to the record will be the string colFullName instead of the the selected value. I am going through my update agains to make sure that I am not doing something of dubious nature there.
I do have a couple of other methods I could use to do the same thing as I want to here, but this method, if I can get it right, is direct and much easier.
Attachment 188589
-
Aug 28th, 2023, 08:52 PM
#10
Re: Combobox routine failure
The screenshot didn't attach properly. Never done one myself, but I've seen numerous other people say that attachments are broken when posting a quick reply and you have to click "Go Advanced" and attach it there.
I'm pretty sure part of what you are describing (and has already been pointed out earlier in this thread) is because of the line:
Code:
cboOwner.Text = ContactName
Comment that line out.
-
Aug 28th, 2023, 09:15 PM
#11
Re: Combobox routine failure
To set the display value, and the hidden value…
ComboBox1.DisplayMember = “a field name”
ComboBox1.ValueMember = “a different field name”
To change the selectedIndex of the bound ComboBox1…
You could use ComboBox1.SelectedValue = “01”
Notice I used a string in accordance with your fields datatype
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 28th, 2023, 09:35 PM
#12
Re: Combobox routine failure
A couple of questions.
Again,, is the combobox itself bound? If so, please show how it's setup.
Are you trying to display a new record or and existing one?
-
Aug 28th, 2023, 09:40 PM
#13
Re: Combobox routine failure
 Originally Posted by gwboolean
I just found that out and have already changed it back. I do have some more information.
I found that the combobox does indeed have the list of contact names (colFullName).
1. The combobox is displaying, "colFullName" as cboOwner.txt. Having said that, I could not find where cboOwner.Text was assigned a value when the controls were bound. However, whatever I selected from the combobox list was saved (if I save it) as the string "colFullName".
cboOwner.Text will be the displayed value that's sselected... if your'e looking for the "hidden" value.... then use cboOwner.Value ... that will return the value that's bound to the column spacified by the .ValueMember setting ...
-tg
-
Aug 28th, 2023, 09:46 PM
#14
Re: Combobox routine failure
 Originally Posted by techgnome
if your'e looking for the "hidden" value.... then use cboOwner.Value
SelectedValue, not Value.
-
Aug 28th, 2023, 09:59 PM
#15
Re: Combobox routine failure
 Originally Posted by gwboolean
When the code is executed, the column names are expressed for the values of both cboOwner.DisplayMember, and cboOwner.ValueMember. e.g., "colFullName", and "colContactID"
Since this is a new approach for me, I have been unable to figure what I might have done to cause this issue.
There is no issue.
The DisplayMember is the NAME of a column or property from which the control draws the data to display. The VALUES from that column or property are accessed via the Text property of the control. If the user selects an item, the Text property contains the value from the column or property of the SelectedItem named in the DisplayMember. If you set the Text property in code, assuming that the DropDownStyle is set to DropDownList, then the item that contains that value in the column or property named in the DisplayMember will be selected.
There is no issue.
The ValueMember is the NAME of a column or property from which the control draws the data to identify the selected item. The VALUES from that column or property are accessed via the SelectedValue property of the control. If the user selects an item, the SelectedValue property contains the value from the column or property of the SelectedItem named in the ValueMember. If you set the SelectedValue property in code then the item that contains that value in the column or property named in the ValueMember will be selected.
Here's a code example that demonstrates the above:
vb.net Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim table As New DataTable
table.Columns.Add("Id", GetType(Integer))
table.Columns.Add("Name", GetType(String))
table.Rows.Add(1, "First")
table.Rows.Add(2, "Second")
With ComboBox1
.DisplayMember = "Name"
.ValueMember = "Id"
.DataSource = table
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ComboBox1.Text = "First"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ComboBox1.SelectedValue = 2
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MessageBox.Show($"{ComboBox1.SelectedValue} {ComboBox1.Text}")
End Sub
You can make selections via the UI or via those Buttons and see how the item selection changes.
-
Aug 29th, 2023, 11:27 AM
#16
Thread Starter
Fanatic Member
Re: Combobox routine failure
I'm curious if the combobox is also bound?
The combobox is not bound. I generally do not do that with a combobox. The original method I was using (see original post), does not do that.
The screenshot didn't attach properly.
I will try it again.
Attachment 188590
To set the display value, and the hidden value…
OK. Could I then do this?
cbo.Owner.ValueMember = "FieldName"
cbo.Owner.SelectedValue = cbo.Owner.ValueMember
That seems logical to me, but I have not tried that yet.
-
Aug 29th, 2023, 11:29 AM
#17
Thread Starter
Fanatic Member
Re: Combobox routine failure
Attachment 188591
Still trying to get that screenshot to show. I never had this problem before.
-
Aug 29th, 2023, 11:36 AM
#18
Re: Combobox routine failure
 Originally Posted by gwboolean
Still trying to get that screenshot to show. I never had this problem before.
The Insert Image tool on the basic editor doesn't work. You have to click Go Advanced and then Manage Attachments.
-
Aug 29th, 2023, 11:39 AM
#19
Re: Combobox routine failure
 Originally Posted by gwboolean
The combobox is not bound.
If you set the DataSource property then it's bound. That's what data binding is.
-
Aug 29th, 2023, 11:44 AM
#20
Re: Combobox routine failure
 Originally Posted by gwboolean
OK. Could I then do this?
cbo.Owner.ValueMember = "FieldName"
cbo.Owner.SelectedValue = cbo.Owner.ValueMember
That seems logical to me, but I have not tried that yet.
No, because that makes absolutely no sense at all. Did you read what I posted? The value you assign to ValueMember is the NAME of a column or property and the SelectedValue is a value from the column or property with that name. Look at the example I provided in post #15. I assigned "Id" to ValueMember because the values were supposed to come from the 'Id' column. Line 22 assigns the value 2, i.e. one of the values from the 'Id' column, to the SelectedValue and that selects the item with that 'Id'. Line 26 displays the current SelectedValue which, had you tested the example, you would have seen was either 1 or 2, i.e. the values in the 'Id' column.
-
Aug 29th, 2023, 12:59 PM
#21
Re: Combobox routine failure
The combobox is not bound. I generally do not do that with a combobox. The original method I was using (see original post), does not do that.
Your original post is not the type of binding I'm talking about (as I said in post #8). I'm not talking about the item collection.
When you display a record from the database, how do you assign the value to the combobox? Or do you?
Are you binding the combobox?
Code:
Combobox1.DataBindings.Add("Text", somebindingsource, "fieldName")
What I'm trying to find out is what other places in your code impact the combobox.
Last edited by wes4dbt; Aug 29th, 2023 at 02:37 PM.
-
Aug 29th, 2023, 03:27 PM
#22
Thread Starter
Fanatic Member
Re: Combobox routine failure
I have gotten everything to work as intended. There were a lot more functions/routines effected by this change than I anticipated.
I'm curious if the combobox is also bound? Not talking about the item list.
I was wrong. It was indeed bound. However, it had been Rem'd out and I had missed it.
I did, thanks.
I appreciate the suggestions, they all applied. I think the lesson learned though, is that there are NO small changes. Damn, I just couldn't believe how many things were affected by that seemingly simple change in one method.
I am a believer now.
-
Aug 29th, 2023, 08:10 PM
#23
Re: Combobox routine failure
 Originally Posted by wes4dbt
Your original post is not the type of binding I'm talking about (as I said in post #8). I'm not talking about the item collection.
When you display a record from the database, how do you assign the value to the combobox? Or do you?
Are you binding the combobox?
Code:
Combobox1.DataBindings.Add("Text", somebindingsource, "fieldName")
What I'm trying to find out is what other places in your code impact the combobox.
I misunderstood what you were getting at there too. I thought you were referring to using DataSource rather than Items. With regards to what you are referring to, it would almost certainly be the SelectedValue that you bound if you have set the ValueMember, as it would generally be a representation of a foreign key relation.
-
Aug 29th, 2023, 08:25 PM
#24
Re: Combobox routine failure
 Originally Posted by jmcilhinney
I misunderstood what you were getting at there too. I thought you were referring to using DataSource rather than Items. With regards to what you are referring to, it would almost certainly be the SelectedValue that you bound if you have set the ValueMember, as it would generally be a representation of a foreign key relation.
Your right. I chose the "Text" property in my example on purpose, I was guessing that the combobox was improperly bound. I had made that mistake in the past.
-
Aug 29th, 2023, 08:58 PM
#25
Re: [RESOLVED] Combobox routine failure
I appreciate the suggestions, they all applied. I think the lesson learned though, is that there are NO small changes. Damn, I just couldn't believe how many things were affected by that seemingly simple change in one method.
Yes changes can be tricky. As tg has pointed out your data access technique can make problems more likely.
Try to localize your data for something like this, there is no reason to use a program wide datatable.
Code:
Private Sub LoadCBItems()
Dim da As New OleDbDataAdapter("Select UserId, [name] from Users", con)
Dim dt As New DataTable
da.Fill(dt)
Me.UserIdComboBox.DisplayMember = "name"
Me.UserIdComboBox.ValueMember = "userId"
Me.UserIdComboBox.DataSource = dt
End Sub
-
Aug 29th, 2023, 09:36 PM
#26
Re: [RESOLVED] Combobox routine failure
 Originally Posted by wes4dbt
As tg has pointed out your data access technique can make problems more likely.
The fact that this rather dodgy DAL has formed part of so many questions lately is part of the issue with trying to solve them. No question should include both that DAL and UI. If there's an issue getting the data then we should be shown the data access code only, so we can see the relevant inner workings of that DAL. If the DAL is getting the correct data it is irrelevant to the question and shouldn't be included. If the issue is getting data from a DataTable into a ComboBox then all we should see is a DataTable and a ComboBox. The opaque method by which the DataTable is populated only serves to obfuscate the actual issue. Breaking down a problem into parts to determine where the actual issue is a critical part of software development and that is something that needs to be worked on here. This DAL should be fully tested independently to confirm it works as expected and then it becomes irrelevant to any questions about how the data it returns is used. If you're trying to diagnose issues with a DAL and a UI at the same time, you're doing it wrong from the outset.
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
|