2

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.

Note:I do not have control over how the data comes into the DropDownList - I cannot modify the SQL.

public void populateLocationList()
{
    DataTable dt_locations = (DataTable)daa_addresses.GetDataByEventLocations(int_eventid);

    if (dt_locations != null && dt_locations.Rows.Count > 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 && !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();
        }
    }
}
3

1 Answer 1

0

I have to sort out randomly.

You'd have to shuffle rows, probably with something close to this code (borrowed from @configurator's answer):

internal static class Extensions
{
    internal static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
    {
        T[] elements = source.ToArray();
        // Note i > 0 to avoid final pointless iteration
        for (int i = elements.Length - 1; i > 0; i--)
        {
            // Swap element "i" with a random earlier element it (or itself)
            int swapIndex = rng.Next(i + 1);
            yield return elements[swapIndex];
            elements[swapIndex] = elements[i];
            // we don't actually perform the swap, we can forget about the
            // swapped element because we already returned it.
        }

        // there is one item remaining that was not returned - we return it now
        yield return elements[0]; 
    }
}

Assuming that RemoveDuplicateLocations returns a DataTable the binding part of your code should be changed to:

        ddl_locations.DataSource = RemoveDuplicateLocations(dt_locations)
            .AsEnumerable()
            .Shuffle(new Random())
            .CopyToDataTable();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.