为什么我无法编辑 DataGridView 中的值,即使它未设置为只读?
我有一个 DataGridView,它没有设置为只读。 它的所有列均未设置为 ReadOnly,并且其绑定的对象也未设置为 ReadOnly。 但是,我无法编辑 DataGridView 项目? DataGridView 的 .DataSource 属性设置为 ReadOnlyCollection<>,但我可以通过编程方式更改元素,但不能从 UI 更改元素。 这是怎么回事?
I have a DataGridView, which is not set to ReadOnly. None of its columns are set to ReadOnly, and the object it is bound to is not set to ReadOnly. Yet, I can't edit the DataGridView items? The .DataSource property of the DataGridView is set to a ReadOnlyCollection<>, but I can programmatically alter the elements, just not from the UI. What's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
事实证明,如果您的 DataGridView 绑定到 ReadOnlyCollection,那么即使您可以通过编程方式编辑集合中的任何项目,DataGridView 也会限制您更改值。 我不确定这种行为是否是故意的,但值得警惕。
It turns out that if your DataGridView is bound to a ReadOnlyCollection, then even though you can programatically edit any item in the collection, the DataGridView will restrict you from changing the values. I'm not sure if this behavior is intentional, but its something to watch out for.
我昨天安装了 VS 2013,最新版本(更新 5),但仍然存在导致您描述的行为的错误。
简而言之,要解决该错误,首先确保 datagridview 设置为 GUI 可编辑。 这尤其包括控件右上角表单设计器中的小箭头。 下拉箭头中有一个选项“启用编辑”,确保已启用它。 现在,在表单设计器中以某种主要方式编辑列(例如添加或删除列)。 就是这样,当您运行该程序时,您应该会发现 GUI 编辑现在正在工作。
要重现此错误,请在表单设计器中使用 datagridview 控件右上角的小箭头将“启用编辑”设置为 false。 现在对列进行重大更改(例如添加或删除列)。 编译并运行程序。 现在返回到小箭头并重新启用“启用编辑”复选框。 再次运行程序。 此时,错误就会显现出来,您会发现 datagridview 不可进行 GUI 编辑,即使您在 VS 中进行了其他配置。
I installed VS 2013 just yesterday, latest build (update 5) and a bug still remains that causes the behavior you describe.
In short to work around the bug, first make sure the datagridview is set to be GUI-editable. This especially includes the tiny arrow in the form designer at the top right of the control. In the arrow drop down is an option "enable editing", ensure it's enabled. Now in the form designer edit the columns in some major manner (such as add or remove a column). That's it, when you run the program you should find the GUI editing is working now.
To reproduce this bug, within the form designer use the tiny arrow at the top right of the datagridview control to set "enable editing" to false. Now make a major change to the columns (such as add or remove a column). Compile and run the program. Now go back to the tiny arrow and re-enable "enable editing" checkbox. Again run the program. At this point the bug manifests itself, and you will find that the datagridview is not GUI-editable even though you've configured otherwise in the VS.
这只是一个扩展注释(因此是 wiki),与“DataGridView 将限制您更改某些值(字符串)但不更改其他值(布尔值)”点相反; 两者都不可编辑; 将其设为
List
并且两者均可编辑...:This is just an extended comment (hence wiki) in counter to the "the DataGridView will restrict you from changing some values (strings) but not other values (bools)" point; neither is editable; make it a
List<T>
and both are editable...:您如何绑定到 DataGridView? 有一件事是,如果您使用 Linq 列表作为从数据库查询的数据源,并且没有完整的对象,则属性是只读的,除非您在 select 函数中指定“with new”。 您的帖子中没有太多信息。 我希望这有帮助。
How are you binding to your DataGridView? One thing is that if you are using a Linq list as the datasource queried from a database and you do not have the complete object, then the properties are readonly unless you specify "with new" in the select function. There is not a lot of information in your post. I hope this helps.
如果您已将网格绑定到已全局定义的集合,请尝试在您绑定的点(函数)中创建该集合的本地克隆副本,并将网格绑定到新集合。 这对我有用。
If you have bound the grid to a collection which has been defined globally , try to create a local clone copy of the collection in the spot(function) you are binding in and bind the grid to the new collection. That worked for me.
没有什么对我有用。 我没有使用绑定。 只是将所有内容设置为非只读并将编辑模式设置为(尝试了所有值)。 这些单元格始终是只读的。
我使其工作的方法是在事件处理程序中将单元格设置为 ReadOnly = false:
在此处理程序结束时,单元格处于编辑模式(我单击的单元格)。 在 GUI 中进行更改,其余部分(如果有)在 CellEndEdit 事件处理程序中处理。 当鼠标移出单元格或按 Enter 键时,将调用此处理程序。
此时新值保存在CurrentCell.Value中;
Nothing worked for me. I didn't use binding. Just made everything NOT read only and set edit mode to (tried all values). The cells were always read-only.
The way I made it work was to set the cell to ReadOnly = false in the event handler:
At the end of this handler, the cell is in edit mode (the cell I clicked on). Made the changes in the GUI and the rest (if any) is handled in the CellEndEdit event handler. This handler is called when the mouse goes out of cell or you press Enter.
At this point the new value is saved in CurrentCell.Value;
执行
datagridview
任务并单击编辑列,然后将列只读属性设置为 False< /a>Take
datagridview
task and click on edit column then set the column Read Only Property to False