开发者

How to bind two columns values in dropdown with separate comma(,)

开发者 https://www.devze.com 2023-03-15 03:19 出处:网络
I have a asp.net a开发者_开发百科pplication in that I am using ado.net entity framework in which I want to bind the two column in that dropdown.for example:

I have a asp.net a开发者_开发百科pplication 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();
    }
}


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.


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();


try this

    ddl.Items.Clear();
    foreach (var item in myCollection)
    {
        ddl.Items.Add(new ListItem(item.FirstName + "," + item.LastName, item.ID));
    }


DataSet ds = MyDataSet();

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

            dropLojas.Items.Add(ds.Tables[0].Rows[i]["COLUNA1"].ToString() + ds.Tables[0].Rows[i]["COLUNA2"].ToString() + ds.Tables[0].Rows[i]["COLUNA3"].ToString());
            dropLojas.DataValueField = ds.Tables[0].Rows[i]["COLUNA1"].ToString();

        }

You can do best with this.


protected void BindSalesOrganization()
{
    IList<Customer> objCustomerList = (new CustomerFacade()).GetAllSalesOrgByUserCode(SessionManager.UserCode);
    if (objCustomerList != null && objCustomerList.Count >= 0)
    {
        ddlSalesOrg.Items.Clear();
        ddlSalesOrg.DataSource = objCustomerList;
        ddlSalesOrg.DataTextField = "SalesOrganization";
        ddlSalesOrg.DataValueField = "SalesOrgCode";
        ddlSalesOrg.DataBind();
    }
}


CREATE PROCEDURE [dbo].[sp_GetAllSalesOrgsByUserCode]                                    
@UserCode VARCHAR(50)                                          
AS                                          
BEGIN                            

SELECT S.code,'[' +  S.Code + '] '+ S.[Description]   AS description  FROM tblSalesOrganization S         
WHERE S.IsActive=1           

END            
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号