6
Contributors
6
Replies
17
Views
11 Years
Discussion Span
Last Post by jugosoft
0

If you are using a SELECT statement, which I would assume you are, just add an ORDER BY clause to that statement and voila.

0

Ok, you are a bit unclear here with the issue you are facing.
Would you like to populate listBox with already sorted items, or would you like to sort items, which are already in the listBox?

1

Here is the code which shows you both:
(ps: button click even reverses the list, so you can see the difference in the code)

public Form1()
        {
            InitializeComponent();
            string[] array = new string[] { "D", "C", "A", "E", "B" };
            Array.Sort(array);
            listBox1.Items.AddRange(array);
        }

        private void buttonInvert_Click(object sender, EventArgs e)
        {
            int rows = listBox1.Items.Count;
            string[] array = new string[rows];
            for(int i=0;i<listBox1.Items.Count;i++)
            {
                array[i] = listBox1.Items[i].ToString();
            }
            Array.Reverse(array);
            listBox1.Items.Clear();
            listBox1.Items.AddRange(array);

        }
0

You can set ListBox's property 'Sorted' to 'true'. If you are using an array, make sure that sorting doesn't change combination of members. If you are using an array, there is a sort function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.