Vikrant Mishra
Vikrant Mishra

Reputation: 61

Custom Sort in datagridview in winform

I have data table having rows like

ID Name
2  A
4  B
3  C
5  D
1  E

List order = new List() { "1", "3", "2", "5", "4" }

--------------order by list-----------------

ID Name
1   E
3   C
2   A
5   D
4   B 

can anyone help to implement this.. I am using DataTable in Winforms.

Upvotes: 2

Views: 382

Answers (3)

roemel
roemel

Reputation: 3297

Another solution to the already given ones, would be to loop your order list and then sort your source list.

// source list
List<Foo> lSource = new List<Foo>() {
    new Foo() { ID = 2, Name = "A" },
    new Foo() { ID = 4, Name = "B" },
    new Foo() { ID = 3, Name = "C" },
    new Foo() { ID = 5, Name = "D" },
    new Foo() { ID = 1, Name = "E" },
};

// order list
List<int> order = new List<int>() { 1, 3, 2, 5, 4 };

// loop order list and sort source list
order.ForEach(x =>
{
    lSource = lSource.OrderBy(g => g.ID == x).ToList();
});

// set datasource
dataGridView1.DataSource = lSource;

I just added a class Foo containing an int ID and a string Name, because you didn't share your whole code.

Upvotes: 4

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98740

I think you can join your order and your datatable with AsEnumerable method and on on part you can equalize both of them and select rows, then you can generate a DataTable from that query with CopyToDataTable method.

var dt = new DataTable();
var dc = new DataColumn() { ColumnName = "ID", DataType = typeof(string) };
dt.Columns.Add(dc);
dc = new DataColumn() { ColumnName = "Name", DataType = typeof(string) };
dt.Columns.Add(dc);
dt.Rows.Add(new object[] { "2", "A" });
dt.Rows.Add(new object[] { "4", "B" });
dt.Rows.Add(new object[] { "3", "C" });
dt.Rows.Add(new object[] { "5", "D" });
dt.Rows.Add(new object[] { "1", "E" });

List<string> order = new List<string>() { "1", "3", "2", "5", "4" };

var query = from item in order
            join row in dt.AsEnumerable() on item equals row.Field<string>("ID")
            select row;

var result = query.CopyToDataTable();

result will be;

enter image description here

I'm not sure this is the best way or not but this seems to fit with your case.

Upvotes: 2

venerik
venerik

Reputation: 5904

You can join both lists (the one with items and the one with sorted id's) and then select the items:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var list = new List<Item>{
            new Item { Id = 2, Text = "A" },
            new Item { Id = 4, Text = "B" },
            new Item { Id = 3, Text = "C" },
            new Item { Id = 5, Text = "D" },
            new Item { Id = 1, Text = "E" }
        };
        var sortorder = new List<int> { 1, 3, 2, 5, 4 };

        var sortedlist = sortorder.Join(list, x => x, y => y.Id, (x,y) => y);

        foreach(var item in sortedlist) 
            Console.WriteLine("{0} {1}", item.Id, item.Text);
    }
}

Upvotes: 0

Related Questions