按列内容对Datagrid进行排序 [英] Sort Datagrid by Column contents
问题描述
我想根据列内容在网格中订购数据。
我已经尝试了指令( OperationGrid 是我的grid的名称,以及 Part Number 第一列的标题)
I want to order the data in a grid based on a column contents.
I have tryed the instruction (OperationGrid is my grid''s name, and "Part Number" the title of the first column)
OperationGrid.Sort(OperationGrid.Columns["Part Number"], ListSortDirection.Ascending);
在这种情况下,我收到错误
价值不能为空。
参数名称:dataGridViewColumn
不知道错误来自哪里?
和
In that case, I get the error
"Value cannot be null.
Parameter name: dataGridViewColumn"
Any idea of where can the error come from?
and also
OperationGrid.Sort(OperationGrid.Columns[0], ListSortDirection.Ascending);
现在我收到错误 DataGridView控件必须绑定到要排序的IBindingList对象。
知道错误来自哪里?
也许我忘了要定义的东西?但网格工作正常而没有排序。
我相信问题在于我忘记了要声明的内容或类型可以是错误。有什么想法吗?
提前谢谢。
David
Now I get the error "DataGridView control must be bound to an IBindingList object to be sorted".
Any idea of where can the error come from?
Maybe I forgot something to define? but the grid works fine without sorting.
I believe that the problem is that I have forgotten something to declare or a type can be wrong. Any idea about that?
Thank you in advance.
David
推荐答案
试试这个链接:
以编程方式排序DataGrid [ ^ ]
try this link:
Sorting DataGrid programmatically[^]
定义IComparable功能并在课程中创建比较功能:
Define the IComparable feature and create the comparison feature inside the class:
public class Component : IComparable<component>
{
private string PartNumber;
...
public int CompareTo(Component toSort)
{
return PartNumber.CompareTo(toSort.PartNumber)
}
...
}
</component>
用法:
Usage:
private void ListSort(list<component> _list2Sort)
{
_list2Sort.Sort();
}
</component>
这篇关于按列内容对Datagrid进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!