Sorting DataGridView ComboBox Values

382 views Asked by At

i've been trying to sort out some values that i currently add to the comboboxcell through a loop and i use the sort with the dataview however this is only sorting through the 1 number of the value and not taking into consideration the rest of the numbers.

Sample code:

1

Result:

2

What i'm trying to obtain is the following example: 1, 10, 100, 200, instead of, 1, 11, 110, 2, 20, 23, 3 and so forth. If anyone got any ideas.

Thanks

2

There are 2 answers

6
大陸北方網友 On BEST ANSWER

You can use LINQ to sort it and get the "MATERIALPROFILE" list.

Here is a simple demo that setting combobox data dousrce.

List<decimal> datasource = dt.AsEnumerable()
    .OrderBy(r => r.Field<decimal>("MATERIALPROFILE"))
    .Select(r => r.Field<decimal>("MATERIALPROFILE"))
    .ToList();


DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.DataSource = datasource;

dataGridView1.Columns.Add(combo);

Update:

Modify OrderBy clause, like:

.OrderBy(r => Convert.ToInt32(r.Field<string>("FieldName").Split('x')[0].Trim()))
1
Jacob On

You could use LINQ:

DataTable sortedByValue = sortedDT.AsEnumerable()
                   .OrderBy(r=> r.Field<decimal>("MATERIALPROFILE"))
                   .CopyToDataTable();