Sorting a DropDownList? - C#, ASP.NET

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

提交回复
热议问题