【问题标题】:DataGridView and List<T> and sortingDataGridView 和 List<T> 和排序
【发布时间】:2011-12-17 02:13:20
【问题描述】:

我想对我的 DataGridView 进行排序。 我试过这个:

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

还有这个:

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

每次我得到一个错误提示:

DataGridView 控件必须绑定到要排序的 IBindingList 对象。

我需要做什么才能让它工作?

【问题讨论】:

  • 你用什么方法对List&lt;T&gt;DataGridView进行排序?

标签: c# .net list collections datagridview


【解决方案1】:

如果您不想实现IBindingList,请使用List&lt;T&gt; 排序方法。

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

【讨论】:

  • 如果我希望能够根据 Player 类的不同属性进行排序怎么办?例如。年龄、性别、城镇、武器?
  • @Hooch - 是否可以将 List 转换为 DataTable。 DataTable 使用 DataView(IBindingList 由 DataView 实现))允许您对任何表达式(列)进行排序,而无需任何额外代码。
猜你喜欢
  • 2010-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
相关资源
最近更新 更多