Haim Evgi
Haim Evgi

Reputation: 125486

c# add a list of items to combobox ordered by string not natural

i use this code to add a numbers to combobox

 for (int i = 15; i < 250; i++)
 {
   cbSumFrom.Items.Add(i);
 }

the problem is that i get something like

100

101

......

but i want like

15

16

17

......

how to fix it ?

Upvotes: 2

Views: 3115

Answers (3)

Paul Sasik
Paul Sasik

Reputation: 81459

Take a look at your ComboBox.Sorted property. If it is True then you get your unwanted behavior (default, string-based sort.) Since you are populating the combo box from what looks like a presorted list, make sure that ComboBox.Sorted is set to False.

Upvotes: 1

Iain Ward
Iain Ward

Reputation: 9936

The problem is is that it appears the combo box is sorting the item and it's doing an ASCII comparison on each character to do it, so 100 comes before 15 because 10 is before 15. Take the sorting off the combo box and it should list them in the order you;ve added them

Upvotes: 2

m.qayyum
m.qayyum

Reputation: 418

Try this...didn't tested it but try this...

 cbSumFrom.Items.Clear();
 for (int i = 15; i < 250;)
     {
       cbSumFrom.Items.Add(Convert.toString(i));

     }

Upvotes: 1

Related Questions