1

I have combobox where users can input text or select from a list. When a user inputs their own text, instead it showing up at the bottom or top of the dropdown list, I want it to appear in the correct order. For example if a user types in 24 I want it to apear between 20 and 30.

private void LoadComboBox()
    {
        if (ddlTypeUnits.SelectedValue == "HP")
        {
            MotorSizeThreePhase[] motors = MotorSizeThreePhaseFactory.GetList(ActingMotorType, IsHPorBTU, IsAC, true, Common.GetConnectionString());

            cmbOutputRating.DataSource = motors;
            cmbOutputRating.DataTextField = "MotorSizeHP";
            cmbOutputRating.DataValueField = "MotorSizeHP";
            cmbOutputRating.DataBind();

        }

        ThreePhaseMotorLoad curLoad = (ThreePhaseMotorLoad)this.LoadObject;
        ListItem item = new ListItem(curLoad.Size.ToString()); //gets the stored size value
        if (!cmbOutputRating.Items.Contains(item))  //add the size value to the dropdown list
        {
            cmbOutputRating.DataBind();
            cmbOutputRating.Items.Add(item);
            cmbOutputRating.Text = curLoad.Size.ToString();
        }
    }
ArtisanSamosa
  • 847
  • 2
  • 10
  • 23

2 Answers2

1

C# - is it possible to arrange ComboBox Items from a to z?

If you're using Win Forms, just use ComboBox.Sorted = true;

If the data in your combo box comes from in a form of a list, just use OrderBy to the List of data you are going to put in the ComboBox before putting it. example:

    List<string> a = new List<string>()
    {
        "q",
        "w",
        "e",
        "r",
        "t",
        "y",
        "u",
        "i",
        "o",
        "p",
        "a",
        "s",
        "d",
        "f",
        "g",
        "h",
        "j",
        "k",
        "l",
        "z",
        "x",
        "c",
        "v",
        "b",
        "n",
        "m",
    };

    comboBox1.Items.AddRange(a.OrderBy(c => c).ToArray());
Community
  • 1
  • 1
  • The data comes from motors array, which I can't convert to a list because of its protection level. Do you know of any ways to take those values and put them to a list, sort, and then bind? Or is there a way to sort cmbOutputRating.Items? – ArtisanSamosa Jun 25 '15 at 18:28
  • setting this propoerty did not work? ComboBox.Sorted = true; – Miroslav Bihari Jun 25 '15 at 18:31
  • i've just tried this and it worked for me fine comboBox1.Items.Add("a"); comboBox1.Items.Add("d"); comboBox1.Items.Add("y"); comboBox1.Items.Add("b"); comboBox1.Items.Add("e"); comboBox1.Sorted = true; – Miroslav Bihari Jun 25 '15 at 18:34
  • I'm using a Ajax Control Toolkit ComboBox, not a winforms one. This doesn't have the .sorted property. – ArtisanSamosa Jun 25 '15 at 18:37
  • https://msdn.microsoft.com/en-us/library/6tf1f0bc(v=vs.110).aspx at first, you have to sort the array, that is the datasource. when adding a new item, dont add it to combobox.items but add it to the array, sort it again and bind it again. hope you get what i mean :) – Miroslav Bihari Jun 25 '15 at 18:43
  • thanks for the help. Sorry if I'm explaining anything wrong. I get what you mean and that's how I originally intended to do it, but I can't actually add anything to that array or convert it to a list due to its protection level. I can't mess with any of the data in there. The user text needs to just temporarily show up in the drop-down list in the correct order. Do you have any suggestions on how to accomplish this without changing the motors array? – ArtisanSamosa Jun 25 '15 at 18:55
  • couldn't you create a new list from the motors array (copy) and add it to the new list? var list = new List(motors); or through a foreach copy all the items in array to new list. and then switch the source to the list? – Miroslav Bihari Jun 25 '15 at 18:59
  • Miroslav, I was able to get it to work by running a loop and just inserting it into the correct index to begin with. – ArtisanSamosa Jun 29 '15 at 18:43
0

Instead of doing items.add() and appending the item to the bottom of the list, I inserted the item into the correct index.

            int newItemIndex = 0;
            foreach (ListItem li in cmbOutputRating.Items)
            {

                if (Convert.ToDouble(li.Value) < curLoad.Size)
                {
                    newItemIndex++;
                }
            }

            cmbOutputRating.Items.Insert(newItemIndex, curLoad.Size.ToString());
ArtisanSamosa
  • 847
  • 2
  • 10
  • 23