该属性将是错误的,如果你有DataGridView中。 AllowUserToAddRows = true;但没有绑定类的默认构造函数。 添加默认值,它应该工作
public class atsTableInclude
{
// keep this to allow user to add row
public atsTableInclude() { }
public atsTableInclude(string p, bool u)
{
Prefix = p;
Use = u;
}
public string Prefix { get; set; }
public bool Use { get; set; }
}
public Sorting.SortableBindingList<T> FillAtsList<T>(string jsonfile) where T : class
{
if (!File.Exists(jsonfile))
{
MessageBox.Show(jsonfile, "File not found");
return null;
}
try
{
// load json from file
using (StreamReader r = new StreamReader(jsonfile))
{
string json = r.ReadToEnd();
var res = JsonConvert.DeserializeObject<List<T>>(json);
return new Sorting.SortableBindingList<T>(res);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Cannot load json", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return null;
}
private void frmATS_Load(object sender, EventArgs e)
{
string jsonfile2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "atsTableInclude.json");
dataGridView2.DataSource = FillAtsList<atsTableInclude>(jsonfile2);
}
你为什么不绑定在'BindingList'上? http://msdn.microsoft.com/en-us/library/ms132679.aspx – Jon 2011-04-07 01:54:10
@Jon我不知道'BindingList',我通常不会在WinForms上编码。将它更改为'BindingList'工作,谢谢。 – BrunoLM 2011-04-07 02:00:32