Search code examples
c#visual-studiosortingdatagridviewdatagridviewcomboboxcell

Sorting DataGridView ComboBox Values


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


Solution

  • 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()))