【发布时间】:2022-01-23 19:02:34
【问题描述】:
由于某种原因,当我使用列表作为绑定源时,dataGridView 不允许我以任何方式对其进行排序。有什么建议可以让字母排序正常工作吗?
public partial class Form : Form
{
List<Person> people = new List<Person>();
string personsfile = "c:\\temp1\\people.json";
public Form()
{
InitializeComponent();
henkilot = DeserializeJSON();
if (people== null)
{
people= new List<Person>();
}
populateDataGrid();
}
public void SerializeJSON(List<Person> input)
{
string json = JsonConvert.SerializeObject(input);
//write string to file
System.IO.File.WriteAllText(personsfile, json);
}
public List<Person> DeserializeJSON()
{
if (File.Exists(personsfile))
{
using (StreamReader r = new StreamReader(personsfile))
{
string json = r.ReadToEnd();
return JsonConvert.DeserializeObject<List<Person>>(json);
}
}
else
return null;
}
public void populateDataGrid()
{
BindingSource source = new BindingSource();
source.DataSource = people;
dataGrid.DataSource = source;
}
【问题讨论】:
-
看看这篇文章:DataGridView - Sort Generic Lists by Click on Column Headers - 要在 DataGridView 中自动支持排序,列表应该实现 IBindingList 及其与排序相关的成员。
标签: c# visual-studio winforms