|
-
Apr 24th, 2007, 06:52 PM
#1
Thread Starter
Addicted Member
[RESOLVED]Value Member & Display member for combo box
A query brings back case_ID and Case name
I want to display case name in the combo box, but when it is selected the value that is passed through to my insert statement i want to be the Case_ID that relates to the case name selected.
i tried this
casename.ValueMember = "Case_ID"
casename.DisplayMember = "Case_Name"
but when message boxing the display member, it just shows the text. Anyone help?
Last edited by HelpLaura; Apr 26th, 2007 at 07:52 PM.
-
Apr 24th, 2007, 07:04 PM
#2
Re: Value Member & Display member for combo box
Bind the combobox to the case datasource.
ComboBox1.DataSource = "Your datasource???"
ComboBox1.DisplayMember = "Case_Name"
-
Apr 26th, 2007, 06:21 PM
#3
Thread Starter
Addicted Member
[RESOLVED] Re: Value Member & Display member for combo box
Hi, i tried this but get the error:
Cannot bind to the new display member.
Parameter name: newDisplayMember
the code i used for this was:
vb Code:
Private Sub frm_communication_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & DBPath & ";")
Dim Comm As System.Data.OleDb.OleDbCommand
Comm = New System.Data.OleDb.OleDbCommand("SELECT case_id, case_name FROM tbl_case ORDER BY case_name", Conn)
Dim DA As New System.Data.OleDb.OleDbDataAdapter
DA.SelectCommand = Comm
Dim DT As DataTable
DA.Fill(DS)
DT = DS.Tables(0)
Dim r As DataRow
For Each r In DT.Rows
Dim i As Integer = Me.CaseName.Items.Add(r("case_name"))
Next
Comm.Dispose()
DA.Dispose()
Conn.Close()
Conn.Dispose()
casename.DataSource = DS
casename.ValueMember = "case_id"
casename.DisplayMember = "case_name"
MessageBox.Show(casename.ValueMember.ToString)
End Sub
Anyone know why this happpening? Have i just define them in thewring place?
Last edited by HelpLaura; Apr 26th, 2007 at 07:48 PM.
Reason: Resolved
-
Apr 26th, 2007, 06:33 PM
#4
Re: Value Member & Display member for combo box
What is "caseName"? if it is a combobox then why are you adding items to it. If you bind it to the DS then it spouse to fill the items automaticly.
-
Apr 26th, 2007, 06:36 PM
#5
Re: Value Member & Display member for combo box
'DS' is your DataSet. Your DataSet doesn't contain anything named "case_id" or "case_name". Your DataSet contains a DataTable named "tbl_case". That DataTable, which is referred to by the 'DT' variable, contains columns named "case_id" and "case_name". You should be assigning that DataTable to the DataSource. It should look like this:
vb Code:
casename.ValueMember = "case_id"
casename.DisplayMember = "case_name"
casename.DataSource = DT
Note that I set the DisplayMember and ValueMember before setting the DataSource.
Note also that you should get rid of this:
vb Code:
Dim r As DataRow
For Each r In DT.Rows
Dim i As Integer = Me.CaseName.Items.Add(r("case_name"))
Next
If you're binding the data to the control then you don't add the data manually too.
Finally, while it's not critiacal either it is good practice to ALWAYS specify the name of the table you're filling:And finally again, as far as I can see from that code there's no point you even using a DataSet. You may as well just use a DataTable on its own. Here's a cleaned up version of that code:
vb Code:
Dim con As New OleDbConnection("connection string here")
Dim adp As New OleDbDataAdapter("SELECT case_id, case_name FROM tbl_case ORDER BY case_name", con)
Dim tbl As New DataTable
adp.Fill(tbl)
adp.Dispose()
con.Dispose()
Me.caseName.DisplayMember = "case_name"
Me.caseName.ValueMember = "case_id"
Me.caseName.DataSource = tbl
Note also that "casename" is probably not the best name for a ComboBox. That variable name implies that it contains the name of a case. That might be true of a String but not a ComboBox. I'd suggest something that more accurately describes its purpose as a way to select a case name, like "caseNameCombo" or "caseNameSelector".
-
Apr 26th, 2007, 07:13 PM
#6
Thread Starter
Addicted Member
Re: Value Member & Display member for combo box
Hi guys thanks for your posts on this.
I have added the code provided ontot he form load event. The combo box is automatically populated with the first value in the combo box. But when i got to display the message box. I still get just case_id rather than the actual number
-
Apr 26th, 2007, 07:20 PM
#7
Re: Value Member & Display member for combo box
ValueMember is the name of the member of the DataSource from which the values should be drawn. Once you assign a value to it, which in your case is "case_id", that is the value that it will always have.
As the name suggests, it's the SelectedValue property of the ComboBox that returns the value of the selected item.
When you get the SelectedValue property it will get the value of the property specified in the ValueMember from the current SelectedItem.
-
Apr 26th, 2007, 07:48 PM
#8
Thread Starter
Addicted Member
Re: Value Member & Display member for combo box
I get what you mean now.
This has been a massive help in my project.
Thanks
-
Apr 27th, 2007, 02:09 AM
#9
Fanatic Member
Re: Value Member & Display member for combo box
Microsoft recommend that you place them in this order or your liable for up to 40% inefficiency
datamember
valuemember
datasource
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
|