C# DataGridView 滚动条大小不正确导致异常
我有一个 DataGridView
并在 for
循环中向其添加几行(约 35 行),同时它的 Enabled
属性设置为 假
。我过去曾这样做过(当 Enabled
设置为 true 时),与 DataGridView
的查看区域相比,垂直滚动条块将更新为适当的大小,并且网格中的项目数。然而现在,它的尺寸似乎不正确。
重新启用 DataGridView 后,我选择一行并“向下箭头”浏览各行,直到程序抛出异常,提示“值应在范围内”并引用滚动条。另外,我发现如果我对任何列进行排序,它会将滚动条块更新为正确的大小。知道发生了什么事吗?
谢谢!
I have a DataGridView
and add several rows to it in a for
loop (~35 rows) while it's Enabled
property is set to false
. I have done this in the past (when Enabled
is set to true) and the vertical scroll bar block would update to the appropriate size compared to the DataGridView
's viewing area and the number of items in the grid. Now, however, it appears but is the incorrect size.
After I re-enable the DataGridView
, I select a row and "arrow-down" through the rows until the program throws an exception saying "Value should be within the range" and is referring to the scroll bar. Also, I discovered that if I sort any of the columns it will update the scroll bar block to the correct size. Any idea what's going on?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚遇到了同样的问题,并按如下方式解决:
DataGridView.SuspendLayout()
...添加行
DataGridView.ResumeLayout( true );
I have just had the same issue and solved it as follows:
DataGridView.SuspendLayout()
... do the adding of the rows
DataGridView.ResumeLayout( true );
我最近遇到了同样的问题。
你可以这样做
1. 首先将DataSource 设置为NULL,然后再次提供Datasource。
或者
(通过手机发布)
I recently faced same issue.
You can either do as
1. First set the DataSource to NULL then provide the Datasource again.
Or
(posted from mobile)
我有完全相同的问题。
我尝试了一切。
最后我决定解决这个问题。当我允许AllowUserToAddRow为True时,滚动开始正常工作。
我选择在代码中处理这个新行,而不是出现错误和不正确的滚动条。也许这对某人有帮助。
I had the exact same issue.
I tried everything.
In the end I settled for a work around. When I allowed AllowUserToAddRow to True the scrolling started working correctly.
I opted to handle this new row in code rather than have the error and the incorrect scrollbar. Perhaps this helps someone.