【问题标题】:how can i sort drop down items in asp.net?如何在asp.net 中对下拉项目进行排序?
【发布时间】:2013-12-16 18:06:47
【问题描述】:

我在 asp.net 中有一个下拉列表,我从数据库中添加了一些内容。最后我还手动添加了一些东西。现在我需要以一种快速简单的方式对这些项目进行排序。下拉选择的值是数字。

对象链接对我的问题有用吗?如果您的回答是肯定的,请描述。

【问题讨论】:

  • 你能粘贴你用来创建下拉菜单的 UI 代码吗?您使用的是 WebForms 还是 MVC?

标签: c# asp.net vb.net


【解决方案1】:

您可以创建一个像这样的小型实用方法来对 DropDownList 的项目进行排序。

public static void SortListControl(ListControl control, bool isAscending)
{
    List<ListItem> collection;

    if (isAscending)
        collection = control.Items.Cast<ListItem>()
            .Select(x => x)
            .OrderBy(x => x.Text)
            .ToList();
    else
        collection = control.Items.Cast<ListItem>()
            .Select(x => x)
            .OrderByDescending(x => x.Text)
            .ToList();

    control.Items.Clear();

    foreach (ListItem item in collection)
        control.Items.Add(item);
}

用法

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
        DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));

    // Sort the DropDownList's Items by descending
    SortListControl(MyDropDownList, false);
}

【讨论】:

    【解决方案2】:

    没有代码就很难回答你的问题。但这很可能是您正在寻找的。 IEnumerable.OrderBy()

    根据某个键对序列中的元素进行升序排序。

    【讨论】:

      【解决方案3】:

      使用这个:

              SortedList<int, string> mySortedList = new SortedList<int, string>();
              mySortedList.Add(1, "Hi");
              mySortedList.Add(2, "Hello");
              mySortedList.Add(3, "German");
      
              dropDownList1.DataTextField = "Value";
              dropDownList1.DataValueField = "Key";
              dropDownList1.DataSource = mySortedList;
              dropDownList1.DataBind();
      

      【讨论】:

        【解决方案4】:

        以简单的方式对下拉列表项进行排序。首先通过适配器填充数据集,然后从数据集中填充数据视图并使用所需列对数据视图进行排序。最后将下拉列表与数据视图绑定。

        第 1 步:创建连接对象,然后使用 dataadapter 填充数据集

        例子:

        I) 创建连接对象如下:

        SqlConnection con=new SqlConnection("Data Source=servername;Database=dbname;Integrated Security=true"); //if windows authentication 
        (or) 
        SqlConnection con=new SqlConnection("Data Source=servername;Database=dbname;user id=xxx;pwd=xxx"); //if sql authentication 
        

        II) 以查询和连接对象为两个参数为适配器类创建对象如下:

        SqlDataAdapter sda=new SqlDataAdapter("query",con);
        

        步骤2:创建DataSet对象并使用适配器对象填充如下:

        DataSet ds=new DataSet();
        sda.fill(ds);// filling sda data into dataset using fill method of adapter class
        

        第 3 步:检查数据集是否为非空。如果非空,则创建 DataView 对象并用排序选项填充它并绑定下拉列表,如下所示:

        if(ds.Tables[0].rows.count>0)
        {
            DataView dv=new DataView();
            dv.Table=ds.Tables[0]; // filling dataview with dataset
            dv.sort="columnname";
            DropDownList1.DataSource=dv;
            DropDownList1.DataTextField="columnname";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0,"select");
        }
        

        【讨论】:

          【解决方案5】:

          您可以将其设置为 SortedList,然后调用 list.Sort() 方法。

          您可以将列表设置为数据源,并将键/值字段用作 DataTextField 和 DataValueField。

          StackOverflow question on sorted list as datasource

          【讨论】:

            猜你喜欢
            • 2010-12-14
            • 2023-03-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-04
            • 1970-01-01
            • 2013-06-08
            相关资源
            最近更新 更多