C# 保持DataGridView自动排序
我有一个C# 保持DataGridView自动排序,c#,datagridview,bindingsource,C#,Datagridview,Bindingsource,我有一个DataGridView,它由SortableBindingList支持 这本质上是一个BindingList,其数据源是一个自定义对象列表。底层自定义对象以编程方式更新 MySortableBindingList允许我按升序或降序对每列进行排序。我通过重载ApplySortCore方法实现了这一点 protected override void ApplySortCore(PropertyDescriptor prop,
DataGridView
,它由SortableBindingList
支持
这本质上是一个BindingList
,其数据源是一个自定义对象列表。底层自定义对象以编程方式更新
MySortableBindingList
允许我按升序或降序对每列进行排序。我通过重载ApplySortCore
方法实现了这一点
protected override void ApplySortCore(PropertyDescriptor prop,
ListSortDirection direction)
当单击列标题时,这很适合排序,但当该列中的单元格按编程方式更新时,它不会自动排序
是否有其他人提出了一个好的解决方案,可以从底层数据源的编程更新中对
DataGridView
进行排序?尝试覆盖OnDataSourceChanged事件
public class MyGrid : DataGridView {
protected override void OnDataSourceChanged(EventArgs e)
{
base.OnDataSourceChanged(e);
MyComparer YourComparer = null;
this.Sort(YourComparer);
}
}
以这一类为例:
public class MyClass : INotifyPropertyChanged
{
private int _id;
private string _value;
public int Id
{
get
{
return _id;
}
set
{
PropertyChanged(this, new PropertyChangedEventArgs("Id"));
_id = value;
}
}
public string Value
{
get
{
return _value;
}
set
{
PropertyChanged(this, new PropertyChangedEventArgs("Value"));
_value = value;
}
}
public event PropertyChangedEventHandler PropertyChanged = new PropertyChangedEventHandler(OnPropertyChanged);
private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// optional code logic
}
}
将以下方法添加到可排序绑定列表:
public class SortableBindingList<T> : BindingList<T>, INotifyPropertyChanged
where T : class
{
public void Add(INotifyPropertyChanged item)
{
item.PropertyChanged += item_PropertyChanged;
base.Add((T)item);
}
void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
this.PropertyChanged(sender, new PropertyChangedEventArgs(String.Format("{0}:{1}", sender, e)));
}
// other content in the method body
}
公共类SortableBindingList:BindingList,INotifyPropertyChanged
T:在哪里上课
{
公共作废添加(INotifyPropertyChanged项)
{
item.PropertyChanged+=item_PropertyChanged;
基础。添加((T)项);
}
无效项\u PropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
this.PropertyChanged(发送方,新的PropertyChangedEventArgs(String.Format(“{0}:{1}”,发送方,e));
}
//方法体中的其他内容
}
并在表单中使用此示例方法:
public Form1()
{
InitializeComponent();
source = new SortableBindingList<MyClass>();
source.Add(new MyClass() { Id = 1, Value = "one test" });
source.Add(new MyClass() { Id = 2, Value = "second test" });
source.Add(new MyClass() { Id = 3, Value = "another test" });
source.PropertyChanged += source_PropertyChanged;
dataGridView1.DataSource = source;
}
void source_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
MessageBox.Show(e.PropertyName);
dataGridView1.DataSource = source;
}
private void button1_Click(object sender, EventArgs e)
{
((MyClass)source[0]).Id++;
}
public Form1()
{
初始化组件();
source=新的SortableBindingList();
Add(new MyClass(){Id=1,Value=“one test”});
Add(new MyClass(){Id=2,Value=“second test”});
Add(newmyclass(){Id=3,Value=“另一个测试”});
source.PropertyChanged+=source\u PropertyChanged;
dataGridView1.DataSource=源;
}
void source\u PropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
MessageBox.Show(如PropertyName);
dataGridView1.DataSource=源;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
((MyClass)源代码[0]).Id++;
}
谢谢您的帮助。我已经尝试过了,但它无法通知我底层数据源的所有编程更改