Sorting a DropDownList? - C#, ASP.NET

前端 未结 23 1049
慢半拍i
慢半拍i 2020-12-08 19:17

I\'m curious as to the best route (more looking towards simplicity, not speed or efficiency) to sort a DropDownList in C#/ASP.NET - I\'ve looked at a few recommendations b

相关标签:
23条回答
  • 2020-12-08 19:50
            List<ListItem> li = new List<ListItem>();
            foreach (ListItem list in DropDownList1.Items)
            {
                li.Add(list);
            }
            li.Sort((x, y) => string.Compare(x.Text, y.Text));
            DropDownList1.Items.Clear();
            DropDownList1.DataSource = li;
            DropDownList1.DataTextField = "Text";
            DropDownList1.DataValueField = "Value";
            DropDownList1.DataBind();
    
    0 讨论(0)
  • 2020-12-08 19:52

    I agree with the folks in sorting your data in the model before populating them to the DropDownList, so if you are populating this from a DB, it is a good thing to get them sorted already there using a simple order by clause, it will save you some cycles in the web server, and I am sure the DB will do it so much faster. If you are populating this from another data source for example, XML file, using LINQ will be a good idea, or even any variation of Array.Sort will be good.

    0 讨论(0)
  • 2020-12-08 19:53

    Take a look at the this article from CodeProject, which rearranges the content of a dropdownlist. If you are databinding, you will need to run the sorter after the data is bound to the list.

    0 讨论(0)
  • 2020-12-08 19:54

    You can use this JavaScript function:

    function sortlist(mylist)
    {
       var lb = document.getElementById(mylist);
       arrTexts = new Array();
       arrValues = new Array();
       arrOldTexts = new Array();
    
       for(i=0; i<lb.length; i++)
       {
          arrTexts[i] = lb.options[i].text;
          arrValues[i] = lb.options[i].value;
    
          arrOldTexts[i] = lb.options[i].text;
       }
    
       arrTexts.sort();
    
       for(i=0; i<lb.length; i++)
       {
          lb.options[i].text = arrTexts[i];
          for(j=0; j<lb.length; j++)
          {
             if (arrTexts[i] == arrOldTexts[j])
             {
                lb.options[i].value = arrValues[j];
                j = lb.length;
             }
          }
       }
    }
    
    0 讨论(0)
  • 2020-12-08 19:56

    If you get a DataTable with the data, you can create a DataView off of this and then bind the drop down list to that. Your code would look something like...

    DataView dvOptions = new DataView(DataTableWithOptions);
    dvOptions.Sort = "Description";
    
    ddlOptions.DataSource = dvOptions;
    ddlOptions.DataTextField = "Description";
    ddlOptions.DataValueField = "Id";
    ddlOptions.DataBind();
    

    Your text field and value field options are mapped to the appropriate columnns in the data table you are receiving.

    0 讨论(0)
  • 2020-12-08 19:56

    is better if you sort the Source before Binding it to DropDwonList. but sort DropDownList.Items like this:

        Dim Lista_Items = New List(Of ListItem)
    
        For Each item As ListItem In ddl.Items
            Lista_Items.Add(item)
        Next
    
        Lista_Items.Sort(Function(x, y) String.Compare(x.Text, y.Text))
    
        ddl.Items.Clear()
        ddl.Items.AddRange(Lista_Items.ToArray())
    

    (this case i sort by a string(the item's text), it could be the suplier's name, supplier's id)

    the Sort() method is for every List(of ) / List<MyType>, you can use it.

    0 讨论(0)
提交回复
热议问题