【发布时间】:2014-05-14 17:22:52
【问题描述】:
我有一个返回 IList 的函数,它是 DataGridView 的数据源。我了解到 DataGridView 不会对 IList 进行排序。我读了This stackoverflow Q&A 并试图实现SortableBindingList。我一定是做错了什么,因为我的 DataGridView 是空的。我还尝试使用 TextBox 访问 SortableBindingSource 中的元素,但什么也没有。
using Microsoft.SqlServer.Management.Controls;
public partial class Form1 : Form
{
IBusinessLayer businessLayer;
IList<Category> categories;
SortableBindingList<Category> catSortable;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
businessLayer = new BusinessLayer();
categories = businessLayer.GetAllCategories();
catSortable = new SortableBindingList<Category>(categories);
categoryBindingSource.DataSource = catSortable;
categoryDataGridView.DataSource = categoryBindingSource;
textBox1.Text = catSortable[0].CategoryName;
}
}
我检查了 Microsoft.SqlServer.Management.Controls,这看起来对吗?
namespace Microsoft.SqlServer.Management.Controls
{
public class SortableBindingList<T> : BindingList<T>
{
public SortableBindingList();
public SortableBindingList(IList<T> list);
protected override bool IsSortedCore { get; }
protected override ListSortDirection SortDirectionCore { get; }
protected override PropertyDescriptor SortPropertyCore { get; }
protected override bool SupportsSortingCore { get; }
protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction);
protected override void RemoveSortCore();
}
}
我非常感谢您的帮助并帮助我学习。感谢大家!
【问题讨论】:
-
我通过创建自己的 SortableBindingList 类来实现这一点,就像在 stackoverflow 示例中一样。我想使用 Microsoft.SqlServer.Management.Controls.dll。怎么了?
标签: c# winforms sorting datagridview ilist