2011-12-17 40 views
1

我想排序我的DataGridView。 我尝试这样做:DataGridView和列表<T>和排序

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

这:

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

每次我得到一个错误说:

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

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

+1

你用什么方法对`List `或`DataGridView`进行排序? – adatapost 2011-12-17 02:45:03

回答

1

如果您不想执行IBindingList,则使用List<T>排序方法。

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

如果我想根据Player类的不同属性进行排序,该怎么办?例如。年龄,性别,城镇,武器? – Hooch 2011-12-17 11:18:12