locked
Concatinate two Values in a dropdown list with a datasource of ListCollection RRS feed

  • Question

  • User709166883 posted

    Anyone please tell me in detail how to setup a multi value dropdownlist from  a list collection...

    Datasource: Listcollection which contains ColorCode and Description...

    how setup a dropdown with Colorcode-Description like BLK-Black...

    then how to capture these selected value as colorcode only by removing description for update purpose...

    Now I am doin like this...

    ddl.datesource=list<datasetvalues> ...contains (colorcode, description)

    ddl.DataTextField = "ColorCode";

    ddl.DataValueField = "ColorCode";

    ddl.databind();

    then selected value should be like this...

    ddlcolor.Items.FindByValue((DataBinder.Eval(formView1.DataItem, "colorCode").ToString())).Selected = true;

    for update:

    ClassA.Color= ddl.selectedvalue();

    Now what I need change to in the above code to get the combination of both..otherwise i need have textbox for description which syncs with ddl..which is bit complex for my level of programming...thanks..


    Sunday, November 7, 2010 2:39 PM

Answers

  • User1224194097 posted

    Add a new property for Color Class with property name Display Text

    private string m_ColorDisplayText = "";
    public string ColorDisplayText
    {
        get
        {
            return m_ColorDisplayText;
        }
        set
        {
            m_ColorDisplayText = value;
        }
    }

    and modify the BL method like this

    public List<IColor> GetColorCode()
    {
        dataset = DAL.GetColorCode();
        List<IColor> collection = new List<IColor>();
        if (dataset != null)
        {
            if (dataset.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dataset.Tables[0].Rows.Count; i++)
                {
                    collection.Add(new Color
                    {
                        ColorCode = dataset.Tables[0].Rows[i]["ColorCode"].ToString(),
                        Description = dataset.Tables[0].Rows[i]["Description"].ToString(),
                        ColorDisplayText = String.Format("{0}-{1}", dataset.Tables[0].Rows[i]["ColorCode"].ToString(),
                                                                    dataset.Tables[0].Rows[i]["Description"].ToString())
                    });
                }
            }
        }
        return collection;
    }

     And Set DropDownList DataTextField to ColorDisplayText and DataValueField to ColorCode.

    DropDownList1.DataTextField = "DisplayText";
    DropDownList1.DataValueField = "ColorCode";
    DropDownList1.DataBind();

    That's it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 7, 2010 5:04 PM

All replies

  • User1224194097 posted

    ddl.datesource=list<datasetvalues> ...contains (colorcode, description)
     

    What is that ListCollection based on? DataSet or a List with two properties ColorCode and Description. You can handle this in many ways. One simple way is creating a temp list with new property like this. I am assuming that listCollection is obtained from a DataTable.

    Add these import statements

    using System.Data;
    using System.Linq;
    DataTable DataTable1 = new DataTable();
    DropDownList1.DataSource = DataTable1.AsEnumerable().Select(a => new
    {
        ColorCode = Convert.ToString(a["ColorCode"]),
        ColorDescription = Convert.ToString(a["ColorDescription"]),
        DisplayText = String.Format("{0}-{1}", Convert.ToString(a["ColorCode"]), Convert.ToString(a["ColorDescription"]))
    }).ToList();
    DropDownList1.DataTextField = "DisplayText";
    DropDownList1.DataValueField = "ColorCode";
    DropDownList1.DataBind();

    You can find the ListItem by Value which is ColorCode and update DB. New Column DisplayText is solely for just showing the description. That's it.

    Sunday, November 7, 2010 3:41 PM
  • User709166883 posted

    hello Santhosh,

    Thanks for your reply...

    Its a data set...came from Dataacess layer and I am converting it to list<colorclass> in business....

    your solution makes some hope for me...and will u please provie me other way..because I am converting datset to list then again i need to store it to datatable....I don't know is it a good practice...can I do the same operation on the list???

    and please show some piece of code how to capture that value on code behind...do i need to any trimming kind of things..I don't know...Thnks again...



    Sunday, November 7, 2010 4:16 PM
  • User1224194097 posted

    will u please provie me other way..
     

    It is difficult to visualize how you are retrieving the values from DataTable/DataSet.

    I am converting datset to list then again i need to store it to datatable....

    You do not need to convert dataset to list and list to datatable etc., using LINQ, you can just build a List with an ADDITIONAL PROPERTY for DISPLAYTEXT. Add a new Property DISPLAYTEXT or some other name. You can just use a DataReader and return a List of ColorClass Objects. That's it. Avoiding DataTables is a different approach altogether. You can use your existing code to add display Text like this

    DropDownList1.DataSource = DataTable1.AsEnumerable().Select(a => new ColorClass()
    {
        ColorCode = Convert.ToString(a["ColorCode"]),
        ColorDescription = Convert.ToString(a["ColorDescription"]),
        DisplayText = String.Format("{0}-{1}"Convert.ToString(a["ColorCode"]), Convert.ToString(a["ColorDescription"]))
    }).ToList();
    DropDownList1.DataTextField = "DisplayText";
    DropDownList1.DataValueField = "ColorCode";
    DropDownList1.DataBind();

    please show some piece of code how to capture that value on code behind...do i need to any trimming kind of things..

     DropDownList1.SelectedValue gives color Code. You do not need any trimming or string manipulation.

    Sunday, November 7, 2010 4:26 PM
  • User709166883 posted

    This is my code..please check this...u can get an idea...Thank You Very Much..

    BLL Method:

    public List<IColor> GetColorCode()
            {
                dataset = DAL.GetColorCode();
                List<IColor> collection = new List<IColor>();

                if (dataset != null)
                {
                    if (dataset.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < dataset.Tables[0].Rows.Count; i++)
                        {
                            collection.Add(new Color
                            {
                                ColorCode = dataset.Tables[0].Rows[i]["ColorCode"].ToString(),
                                Description = dataset.Tables[0].Rows[i]["Description"].ToString()

                            });
                        }
                    }
                }
                return collection;
            }

    DAL returns dataset which is ADO.Net method with connection strings and calling stored procs...etc...I think U can Imagine how this looks like...

    In page codebehind:

    DropDownList ddlColorCode= ((DropDownList)formViewJewelryEditItems.FindControl("ddlColorCode"));
    ddlColorCode.DataSource = BAL.GetColorCode();
    ddlColorCode.DataTextField = "ColorCode";
    ddlColorCode.DataValueField = "ColorCode";
    ddlColorCode.DataBind();



    Sunday, November 7, 2010 4:54 PM
  • User1224194097 posted

    Add a new property for Color Class with property name Display Text

    private string m_ColorDisplayText = "";
    public string ColorDisplayText
    {
        get
        {
            return m_ColorDisplayText;
        }
        set
        {
            m_ColorDisplayText = value;
        }
    }

    and modify the BL method like this

    public List<IColor> GetColorCode()
    {
        dataset = DAL.GetColorCode();
        List<IColor> collection = new List<IColor>();
        if (dataset != null)
        {
            if (dataset.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dataset.Tables[0].Rows.Count; i++)
                {
                    collection.Add(new Color
                    {
                        ColorCode = dataset.Tables[0].Rows[i]["ColorCode"].ToString(),
                        Description = dataset.Tables[0].Rows[i]["Description"].ToString(),
                        ColorDisplayText = String.Format("{0}-{1}", dataset.Tables[0].Rows[i]["ColorCode"].ToString(),
                                                                    dataset.Tables[0].Rows[i]["Description"].ToString())
                    });
                }
            }
        }
        return collection;
    }

     And Set DropDownList DataTextField to ColorDisplayText and DataValueField to ColorCode.

    DropDownList1.DataTextField = "DisplayText";
    DropDownList1.DataValueField = "ColorCode";
    DropDownList1.DataBind();

    That's it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 7, 2010 5:04 PM
  • User709166883 posted

    ohhh..got it...great santhosh...excellent...this thing killing me from yesterday....thanks for n times...

    -Sunny


    Sunday, November 7, 2010 5:07 PM
  • User-233499379 posted

    how is this done in MVC?

     

    Monday, November 22, 2010 7:20 AM