简体   繁体   中英

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

You could use LINQ:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM