• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 740
  • Last Modified:

dropdownlist DataTextField property

Using an asp:DropDownList control on a webform.  The databound has id, lastname, firstname fields, but no fullname  (lastname + ', ' + firstname).  I need the fullname however I am not allowed to alter the sql query to return the fullname...so I think I need to find an alternative to the datatextfield.  
<asp:DropDownList ID="ddlFullName" runat="server"  DataTextField="???" DataValueField="ID"></asp:DropDownList>

Open in new window

0
MariaHalt
Asked:
MariaHalt
  • 2
2 Solutions
 
MlandaTCommented:
you may need to change from using the databinding approach to manually adding items to the list. Something like this:

                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1

                        Dim dr As DataRow = ds.Tables(0).Rows(i)

                        Dim itm As New ListItem(dr("ID"), dr("Firstname") + " " + dr("LastName"))

                        myCombo.Items.Add(itm)

                    Next
0
 
ramkihardyCommented:
Declare a variable named Full Name
String Fullname="";
Exceute the query and get the query in sqldatareader
if(dr.HasRows)
{
while(dr.Read())
{
Fullname=dr["FirstName"].toString() +' '+dr["Lastname"].ToString();
}
}

Then the Fullname variable has the full name of the candidate....
If u want further help ask me...
Regards
Ramki....
0
 
MariaHaltAuthor Commented:
Although I think both solutions above will work, we must use strongly typed code and found my solution here, http://www.ipreferjim.com/site/2009/04/displaying-multiple-fields-in-a-dropdownlists-datatextfield/.  

Basically,

ddlUsers.DataSource = BLL.Employee.GetEmployees()  

2                 .Select(be =>  

3                     new {  

4                         ID = be.Id,  

5                         FullName = String.Format("{0}{1}{2}",  

6                                     be.LastName,  

7                                     (!string.IsNullOrEmpty(be.FirstName) ? ", " : string.Empty),  

8                                     be.FirstName)  

9                         }).AsEnumerable();
0
 
MariaHaltAuthor Commented:
I'm accepting my solution b/c it works for strongly typed code and may someday help others faced with the same problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now