I have a asp.net application in that I am using ado.net entity framework in which I want to bind the two column in that dropdown.for example:
in database their two column of First_name ,Last_name .I want these two columns value come in a single dropdown using C#.
How to do that?
public void BindClients()
{
//To Bind the Client Names for Searching Option
var ddlclientnames = (from ddl in mortgageentity.Clients select ddl).ToList();
if (ddlclientnames.Count() > 0)
{
ddlsearchclient.DataSource = ddlclientnames;
ddlsearchclient.DataValueField = "Client_ID";
ddlsearchclient.DataTextField = "LastName";
ddlsearchclient.DataBind();
}
}
Unless you use the Fullname a lot, I would suggest to use an anonymous type in your select.
This also limits the amount of data that will be done in your select and other overhead.
var ddlclientnames = (from ddl in mortgageentity.Clients
select new { id = ..., FullName = FirstName + Lastname}.ToList();
What you can do is define a custom property on the Object that does this for you:
Edited for clarity with your objects
You would write something like
public partial class Clients
{
public string FullName
{
get { return String.Format("{0}, {1}", LastName, FirstName); }
}
}
This will give you a read only property FullName
on the Clients
Entity.
Then you can do the following
public void BindClients()
{
//To Bind the Client Names for Searching Option
var ddlclientnames = (from ddl in mortgageentity.Clients select ddl).ToList();
if (ddlclientnames.Any)
{
ddlsearchclient.DataSource = ddlclientnames;
ddlsearchclient.DataValueField = "Client_ID";
ddlsearchclient.DataTextField = "FullName";
ddlsearchclient.DataBind();
}
}
I'd also suggest the use of the Any
method instead of ddlclientname.Count > 0
as it doesn't require enumerating the entire collection.