我有一个函数,它返回一个ILIs,并且是DataGridView的DataSource< T >。我了解到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();
}
}
我真的很感谢你的帮助和帮助我学习。谢谢大家!
2条答案
按热度按时间nsc4cvqm1#
试试这个SortableBindingList:
ni65a41a2#
试试这个,不要忘记将SortMode设置为columns。此SortableBindingList在重新打开时保留元素的原始顺序。