Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting a DropDownList?
    primarykey
    data
    text
    <p>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 but they aren't clicking well with me. this drop down is giving me list in alphabetical order. But I have to sort out randomly.</p> <p>Note:I do not have control over how the data comes into the DropDownList - I cannot modify the SQL.</p> <pre><code>public void populateLocationList() { DataTable dt_locations = (DataTable)daa_addresses.GetDataByEventLocations(int_eventid); if (dt_locations != null &amp;&amp; dt_locations.Rows.Count &gt; 0) { // Populate locations dropdown menu // Bind locationsList instead of dt_locations ddl_locations.DataTextField = "Key"; ddl_locations.DataValueField = "Value"; ddl_locations.DataSource = RemoveDuplicateLocations(dt_locations); ddl_locations.DataBind(); string location = ""; // Set the selection based upon the query string if (Request.QueryString["loc"] != null) { location = Request.QueryString["loc"]; locationLbl.Text = Request.QueryString["loc"]; locationID.Value = "-1"; } if (dt_locations.Rows.Count == 1) { location = ddl_locations.Items[0].Text; locationLbl.Text = location; } // Set location in drop down list int int_foundlocation = 0; bool foundLocation = false; foreach (ListItem lsi_item in ddl_locations.Items) { if (lsi_item.Text.ToLower().Trim() == location.ToLower().Trim()) { int_foundlocation = ddl_locations.Items.IndexOf(lsi_item); foundLocation = true; break; } } ddl_locations.SelectedIndex = int_foundlocation; if (ddl_locations.Items.Count == 1) { // Make location label visible. locationLbl.Visible = true; ddl_locations.Visible = false; } else { locationLbl.Visible = false; ddl_locations.Visible = true; } //* defualt location S for short courses *// if (!IsPostBack &amp;&amp; !foundLocation) { ListItem s = ddl_locations.Items.FindByText("S"); int index = 0; if (s != null) { index = ddl_locations.Items.IndexOf(s); } ddl_locations.SelectedIndex = index; ddl_locations.DataBind(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload