将对象列表绑定到Datagridview(Binding List of object to Datagridview)

编程入门 行业动态 更新时间:2024-10-24 13:29:16
将对象列表绑定到Datagridview(Binding List of object to Datagridview)

我无法将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:

更多推荐

本文发布于:2023-08-05 16:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   对象   列表   Datagridview   List

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!