views:

345

answers:

6

I"m populating a DropDownList from a strongly typed list, and that is working fine. The issue is I want to concatenate two of the fields within the list first and then put them in the dropdown. i.e., FirstName + LastName. I've tried a few things that didn't pan out, so can someone give this novice a lil help.

This is an example of what I'm doing.

private List<Customer> _CustomerList = new List<Customer>();

ddlCustomer.DataSource = _CustomerList;
ddlCustomer.DataTextField = "FirstName";
ddlCustomer.DataValueField = "CustomerKey";
ddlCustomer.DataBind();

this works but I need first and last together and I can't manipulate that data in the Customer object.

A: 

You can create a property in Customer class which concatenate FirstName and LastName and you can use that property in your ddlCustomer.DataTextField

Ankit
He says in the post that he can't maniuplate the data in the Customer object (I'm assuming he doesn't have the ability to add a new property)
Justin Niessner
A: 

I have yet to find a good solution to this problem. I've found the following approach to work the best:

  • Create a wrapper class around Customer (either inherit from Customer if possible, or create an entirely new class that holds a Customer object and exposes further properties.

  • Create a property in your NEW class that concatenates the two fields you wish to databind to.

  • Bind the dropdown to a List of your custom object.

Justin Niessner
+9  A: 

Try using an enumeration of an anonymous object created on the fly.

var _CustomerList = customers.Select( c => new {
                                         Name = c.FirstName + " " + c.LastName,
                                         Key = c.CustomerKey
                                    });


ddlCustomer.DataSource = _CustomerList;
ddlCustomer.DataTextField = "Name";
ddlCustomer.DataValueField = "Key";
ddlCustomer.DataBind();

You may have to add a ToList() after the Select, but I think you can bind to an IEnumerable<T>.

P.S. This example requires the .Net 3.5 Framework

tvanfosson
No need to add a ToList() because it will be enumerated once it is binded
Andreas Grech
That's what I thought, but the only examples I could find on MSDN bound to IList, List<T>, Collection<T>, etc.
tvanfosson
Thank you tvanfosson...please forgive my ignorance(I"m from an asp scripting background) var doesn't seem to be a keyword in c# would I use object here ? and the customers.Select, are you saying to access my customer object directly or the list that is returned?padawan
I was assuming .NET 3.5 and C# 3.0. You'll find that var is a keyword in C# 3.0. You'll need .NET 3.5 to use the Select extension method on IEnumerable<T>. The effect will be to create an enumeration of anonymous objects containing data from your Customer object in the Name and Key properties.
tvanfosson
Updated the answer to specify that this example requires the 3.5 framework
Andreas Grech
the one downside to this is that you end up doing that exact code all over the place... might be nice to create an inheritable class with an abstract method.
aronchick
A: 

To separate the UI representation from the underlying data object, you can create a wrapper object around Customer, such as CustomerForUI, with a single property called FullName - and then put a list of CustomerForUI objects into the UI.

Something like:

public class CustomerForUI
{
    private string _FullName;

    public CustomerForUI(Customer c)
    {
       _FullName = c.FirstName + " " + c.LastName;
    }

    public string FullName
    {
       get {return _FullName;}
    }

    public string CustomerKey
    {  ... }
}

and construct a list of CustomerForUI objects called _UIList, and:

ddlCustomer.DataSource = __UIList;
ddlCustomer.DataTextField = "FullName";
ddlCustomer.DataValueField = "CustomerKey";
ddlCustomer.DataBind();
jean
A: 

Is there a reason why you can't manually build up the list items in the drop-down list? Since you're already doing this in code, I don't see why not.

Just enumerate your business object, and use:

ddlCustomer.Items.Add(new ListItem("text" , "value"));

where text is the first and last name concatenated together.

ScottE
A: 

I agree with ScottE...that would seem to be the simplest way, and would have one less object to keep track of.

sunmorgus