1
votes

I want to sort my DataGridView. I tried this:

Grid.DataSource = PlayerList;
Grid.Refresh();

and this:

BindingSource bs = new BindingSource();
bs.DataSource = PlayerList;
Grid.DataSource = bs;

Every time I get an error saying:

DataGridView control must be bound to an IBindingList object to be sorted.

What do I need to do to get it working?

1
What methods you have used to sort the List<T> or DataGridView? - kv-prajapati

1 Answers

1
votes

If you don't want to implement IBindingList then use List<T> Sort method.

 PlayerList.Sort((p,q) => {
                     if (p.Age >= q.Age)
                            return 1;
                        else
                            if (p.Age < q.Age)
                                return -1;
                        return 0;
                    });
    Grid.DataSource = PlayerList;