Sorting a DropDownList? - C#, ASP.NET

前端 未结 23 1046
慢半拍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:45

    I agree with sorting using ORDER BY when populating with a database query, if all you want is to sort the displayed results alphabetically. Let the database engine do the work of sorting.

    However, sometimes you want some other sort order besides alphabetical. For example, you might want a logical sequence like: New, Open, In Progress, Completed, Approved, Closed. In that case, you could add a column to the database table to explicitly set the sort order. Name it something like SortOrder or DisplaySortOrder. Then, in your SQL, you'd ORDER BY the sort order field (without retrieving that field).

    0 讨论(0)
  • 2020-12-08 19:45
    var list = ddl.Items.Cast<ListItem>().OrderBy(x => x.Text).ToList();
    
    ddl.DataSource = list;
    ddl.DataTextField = "Text";
    ddl.DataValueField = "Value"; 
    ddl.DataBind();
    
    0 讨论(0)
  • 2020-12-08 19:47

    If you are using a data bounded DropDownList, just go to the wizard and edit the bounding query by:

    1. Goto the .aspx page (design view).
    2. Click the magic Arrow ">"on the Dropdown List.
    3. Select "Configure Data source".
    4. Click Next.
    5. On the right side of the opened window click "ORDER BY...".
    6. You will have up two there field cariteria to sort by. Select the desired field and click OK, then click Finish.

    enter image description here

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

    You can do it this way is simple

    private void SortDDL(ref DropDownList objDDL)
    {
    ArrayList textList = new ArrayList();
    ArrayList valueList = new ArrayList();
    foreach (ListItem li in objDDL.Items)
    {
        textList.Add(li.Text);
    }
    textList.Sort();
    foreach (object item in textList)
    {
        string value = objDDL.Items.FindByText(item.ToString()).Value;
        valueList.Add(value);
    }
    objDDL.Items.Clear();
    for(int i = 0; i < textList.Count; i++)
    {
         ListItem objItem = new ListItem(textList[i].ToString(), valueList[i].ToString());
         objDDL.Items.Add(objItem);
    }
    

    }

    And call the method this SortDDL(ref yourDropDownList); and that's it. The data in your dropdownlist will be sorted.

    see http://www.codeproject.com/Articles/20131/Sorting-Dropdown-list-in-ASP-NET-using-C#

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

    Try This:

    /// <summary>
    /// AlphabetizeDropDownList alphabetizes a given dropdown list by it's displayed text.
    /// </summary>
    /// <param name="dropDownList">The drop down list you wish to modify.</param>
    /// <remarks></remarks>
    private void AlphabetizeDropDownList(ref DropDownList dropDownList)
    {
        //Create a datatable to sort the drop down list items
        DataTable machineDescriptionsTable = new DataTable();
        machineDescriptionsTable.Columns.Add("DescriptionCode", typeof(string));
        machineDescriptionsTable.Columns.Add("UnitIDString", typeof(string));
        machineDescriptionsTable.AcceptChanges();
        //Put each of the list items into the datatable
        foreach (ListItem currentDropDownListItem in dropDownList.Items) {
                string currentDropDownUnitIDString = currentDropDownListItem.Value;
                string currentDropDownDescriptionCode = currentDropDownListItem.Text;
                DataRow currentDropDownDataRow = machineDescriptionsTable.NewRow();
                currentDropDownDataRow["DescriptionCode"] = currentDropDownDescriptionCode.Trim();
                currentDropDownDataRow["UnitIDString"] = currentDropDownUnitIDString.Trim();
                machineDescriptionsTable.Rows.Add(currentDropDownDataRow);
                machineDescriptionsTable.AcceptChanges();
        }
        //Sort the data table by description
        DataView sortedView = new DataView(machineDescriptionsTable);
        sortedView.Sort = "DescriptionCode";
        machineDescriptionsTable = sortedView.ToTable();
        //Clear the items in the original dropdown list
        dropDownList.Items.Clear();
        //Create a dummy list item at the top
        ListItem dummyListItem = new ListItem(" ", "-1");
        dropDownList.Items.Add(dummyListItem);
        //Begin transferring over the items alphabetically from the copy to the intended drop
         downlist
        foreach (DataRow currentDataRow in machineDescriptionsTable.Rows) {
                string currentDropDownValue = currentDataRow["UnitIDString"].ToString().Trim();
                string currentDropDownText = currentDataRow["DescriptionCode"].ToString().Trim();
                ListItem currentDropDownListItem = new ListItem(currentDropDownText, currentDropDownValue);
        //Don't deal with dummy values in the list we are transferring over
        if (!string.IsNullOrEmpty(currentDropDownText.Trim())) {
            dropDownList.Items.Add(currentDropDownListItem);
        }
    }
    

    }

    This will take a given drop down list with a Text and a Value property of the list item and put them back into the given drop down list. Best of Luck!

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

    DropDownList takes any IEnumerable as a DataSource.

    Just sort it using LINQ.

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