C# DataGridView- 如何启用垂直滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10833087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# DataGridView- How enable vertical scrollbar
提问by Thomas
The DataGridView (Form) holds enough rows that the vertical Scrollbar is shown. But its not enabled. The Silder for Movement is missing and the Button Up and Button Down are greyed out.
DataGridView (Form) 包含足够多的行以显示垂直滚动条。但它没有启用。用于移动的 Silder 丢失,按钮向上和按钮向下显示为灰色。
=> there is a vertical scrollbar but not enabled.
=> 有一个垂直滚动条但未启用。
I tried:
我试过:
- After filling the DataGridView the control is updated.
- Resizing the entire Panel.
- The Frozen attribute is false.
- I a click in a cell i can use the up and down keys to scroll, but the scrollbar will not be enabled.
- 填充 DataGridView 后,控件将更新。
- 调整整个面板的大小。
- Frozen 属性为 false。
- 我在一个单元格中单击我可以使用向上和向下键滚动,但不会启用滚动条。
If I resize the Control while running (DataGridView is on a Splitpanel) the Scrollbar can be used e.g its now enabled.
如果我在运行时调整控件的大小(DataGridView 在 Splitpanel 上),可以使用滚动条,例如它现在已启用。
采纳答案by lolo
Try this:
尝试这个:
1.One of your columns has probably frozen property set as True.
1.您的一列可能已将冻结属性设置为True。
which should be Falsefor all columns.
这应该False适用于所有列。
2.Set the AutoSizeMode of the problematic column to AllCells
2.将问题列的AutoSizeMode设置为AllCells
3.mygrid.DockStyle = DockStyle.Fill
3.mygrid.DockStyle = DockStyle.Fill
回答by Asif
Set ScrollBar property of datagridview is Both.
将 datagridview 的 ScrollBar 属性设置为 Both。
回答by Tobias Knauss
The same problem plus some workarounds can be found here:
DataGridView vertical scrollbar not updating properly (Forms bug?)
It definitely seems to be a winforms bug that appears, e.g. when a DGV is placed inside a tab of a tabcontrol.
可以在此处找到相同的问题以及一些变通方法:
DataGridView 垂直滚动条未正确更新(表单错误?)
这肯定似乎是出现的 winforms 错误,例如,当 DGV 放置在 tabcontrol 的选项卡内时。
回答by tzachs
I had a similar issue with the horizontal scroll bar.
Doing PerformLayouton the grid didn't solve it.
It turns out that the problem in my case was that the form was disabled.
We have an infrastructure that disables the form on load, loads a bunch of stuff asynchronously and at the end enables the form.
For some reason this was enough for the scroll bar to stay disabled when the form was enabled (and like in your scenario resizing the form while running enabled the scroll bar).
So for me the solution was calling PerformLayouton the grid afterenabling the form.
我对水平滚动条有类似的问题。这样做PerformLayout对电网没有解决它。事实证明,我的问题是表单被禁用。我们有一个基础设施,可以在加载时禁用表单,异步加载一堆东西,最后启用表单。出于某种原因,这足以让滚动条在表单启用时保持禁用状态(就像在您的场景中在运行时调整表单大小一样启用滚动条)。所以对我来说,解决方案是在启用表单后调用PerformLayout网格。
回答by Tuyen Nguyen
- You should dock fill your datagridview to panel
- If you have some frozen rows, make sure these rows are set after you finish bringing data to your gridview
- 你应该停靠填充你的 datagridview 到面板
- 如果您有一些冻结的行,请确保在将数据导入 gridview 后设置这些行
If you set the frozen row before, the row when added will take the default style of the first row, and all your gridview rows will have the option row.frozen = true. That's why the vertical scroll won't appear.
如果您之前设置了冻结行,则添加的行将采用第一行的默认样式,并且您的所有 gridview 行都会有选项row.frozen = true。这就是垂直滚动不会出现的原因。

