簡體   English   中英

DataGridView和列表 <T> 和排序

[英]DataGridView and List<T> and sorting

我想對DataGridView進行排序。 我嘗試了這個:

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

和這個:

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

每當我收到一條錯誤消息:

DataGridView控件必須綁定到要排序的IBindingList對象。

我需要怎么做才能使其正常工作?

如果您不想實現IBindingList使用List<T> Sort方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM