Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a employee table(empID, empLname, empFname, empMname) and I want to concatenate two fields in a LnameComboBox. The displayed text in the combobox is empId not the fullname. What's wrong with the code.


Here is my code:

LnameComboBox.DataSource = EmployeeBindingSource
VB
Dim fname As String = "empfname"
            Dim lname As String = "emplname"
            Dim full As String
            full = String.Concat(fname & "," & lname)
            LnameComboBox.DisplayMember = full
            LnameComboBox.ValueMember = "empid"
            EmployeeTableAdapter.Fill(Point_of_saleDataSet11.employee)


Thanks...
Posted
Comments
[no name] 12-Dec-12 4:50am    
whats the error ?
ianshack 12-Dec-12 4:54am    
the displayed text in the combobox is still the empID not the fullname.
choudhary.sumit 12-Dec-12 5:05am    
post your query which you are using to fetch data and make the EmployeeBindingSource.
_Vitor Garcia_ 12-Dec-12 5:34am    
Why don't you use add methos from items like this :
Dim fname As String = "empfname"
Dim lname As String = "emplname"
Dim full As String
full = String.Concat(fname & "," & lname)

LnameComboBox.Items.Add(full)
ianshack 12-Dec-12 19:04pm    
I got an error if I add an Item to a control that Bind to a datasource.

Try this one:

VB
Dim ds As New DataSet
ds =  someDatasource
If ds.Tables(0).Rows.Count > 0 Then
  For index As Integer = 0 To ds.Tables(0).Rows.Count - 1
   LnameComboBox.Items.Add(ds.Tables(0).Rows(index).Item("empfname").ToString() _
   & "," & ds.Tables(0).Rows(index).Item("emplname"))
   LnameComboBox.Items(LnameComboBox.Items.Count - 1).Value = _ ds.Tables(0).Rows(index).Item("empid")
                Next
            End If
 
Share this answer
 
Try this one

VB
LnameComboBox.DataSource = EmployeeBindingSource
Dim fname As String = "empfname"
Dim lname As String = "emplname"
Dim full As String = fname & "," & lname 
LnameComboBox.DataTextField= full
LnameComboBox.DataValueField= "empid"
LnameComboBox.DataBind()
 
Share this answer
 
v2
Comments
ianshack 12-Dec-12 19:03pm    
Sir, It still display the empid not the fullname.
ujju.1 14-Dec-12 4:59am    
Try this one:
Dim ds As New DataSet
ds = EmployeeBindingSource
If ds.Tables(0).Rows.Count > 0 Then
For index As Integer = 0 To ds.Tables(0).Rows.Count - 1
LnameComboBox.Items.Add(ds.Tables(0).Rows(index).Item("empfname").ToString() & ds.Tables(0).Rows(index).Item("emplname"))
LnameComboBox.Items(LnameComboBox.Items.Count - 1).Value = ds.Tables(0).Rows(index).Item("empid")
Next
End If
The DisplayMember should be a public property of the object.
E.g:
C#
// In employee.cs
public string DisplayName 
{
  get { return this.LastName + ", " + this.FirstName; }
}

Then, in your view class:
C#
LnameComboBox.DisplayMember = DisplayName; // pseudo code, won't compile.

You can set this in the Properties (using the designer).

Hope this helps,
Pablo.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900