我无法将List<Person>绑定到DataGridView :
public class Person { public Person(string name, bool likesbacon) { Name = name; LikesBacon = likesbacon; } public string Name { get; } public bool LikesBacon { get; set; } }DataGridView有2列,一个用于Name的TextBoxColumn和一个用于LikesBacon的CheckBoxColumn 。 正确设置DataPropertyName并按预期创建DataGridView 。
public MyForm(List<Person> persons) { InitializeComponent(); dgv.AutoGenerateColumns = false; dgv.DataSource = persons; }但是当我点击CheckBox没有任何反应(dgv的readonly属性设置为false)。 当我逐步调试时,我发现每当鼠标悬停DataGridView的一个单元格时,调试器就会在循环中进入该属性,只要鼠标指针位于单元格上方即可。
因此,我无法通过dgv更改Person的属性。 我究竟做错了什么?
I'm having trouble binding a List<Person> to a DataGridView:
public class Person { public Person(string name, bool likesbacon) { Name = name; LikesBacon = likesbacon; } public string Name { get; } public bool LikesBacon { get; set; } }The DataGridView has 2 columns, a TextBoxColumn for Name and a CheckBoxColumn for LikesBacon. The DataPropertyName is correctly set and the DataGridView is created as expected.
public MyForm(List<Person> persons) { InitializeComponent(); dgv.AutoGenerateColumns = false; dgv.DataSource = persons; }But when I click on the CheckBox nothing happens (the readonly property of the dgv is set to false). When I debug step by step I found that whenever the mouse hovers a cell of the DataGridView the debugger steps into that property in a loop, as long as the mouse pointer is above the cell.
As a result I can't change a Person's properties through the dgv. What am I doing wrong?
最满意答案
将列的ReadOnly属性更改为false后,我能够使其工作。 :捂脸:
After changing the ReadOnly property of the columns to false I was able to make it work. :facepalm:
更多推荐
发布评论