visual-studio DataGridView 中的滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/472563/
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
ScrollBar in DataGridView
提问by Sakkle
I have a winform in vs2008 that contains a DataGridView. The datagrid contains a list with several columns. These are fixed width, exept one that I have set up to take whatever space is left and fill the width of the view. The winform is resizeable in all directions.
我在 vs2008 中有一个包含 DataGridView 的 winform。数据网格包含一个包含多列的列表。这些是固定宽度,除了我设置的一个,以占用剩余的空间并填充视图的宽度。winform 可以在各个方向调整大小。
The issue I am trying to solve is that when I increase the vertical size of the window the scrollbar disappears and the columns snap to the right to fill the extra space. What I would like to happen is that the vertical scrollBar never disappears. Setting ScrollBars to vertical in the properties of the DataGridView does not do this.
我试图解决的问题是,当我增加窗口的垂直大小时,滚动条会消失,并且列会向右对齐以填充额外的空间。我想要发生的是垂直滚动条永远不会消失。在 DataGridView 的属性中将 ScrollBars 设置为垂直不会这样做。
Is this at all possible to achieve? And, if so, how do I get the vertical scrollbar to always be visible?
这完全有可能实现吗?而且,如果是这样,我如何让垂直滚动条始终可见?
回答by gkrogers
Try subclassing the DataGridView and handling the VerticalScrollBar's VisibleChanged event. You should be able to set the Visible property to True in there, overriding the default behaviour.
尝试继承 DataGridView 并处理 VerticalScrollBar 的 VisibleChanged 事件。您应该能够在那里将 Visible 属性设置为 True,覆盖默认行为。
Something like this, I think...
这样的事情,我想...
public class SubclassedDataGridView : DataGridView
    {
        public SubclassedDataGridView (): base()
        {
            VerticalScrollBar.VisibleChanged += new EventHandler(VerticalScrollBar_VisibleChanged);
        }
        void VerticalScrollBar_VisibleChanged(object sender, EventArgs e)
        {
            VerticalScrollBar.Visible = true;
        }
     }
回答by Piotrek
In my case, (re)sorting the grid helped. Try sth like this:
就我而言,(重新)排序网格有帮助。试试这样:
 if (gridName.SortedColumn == null)
   gridName.Sort(gridNameColumns[columnName],ListSortDirection.Ascending);
 else
 {
    ListSortDirection dir;
    if (gridName.SortOrder == SortOrder.Descending) 
       dir = ListSortDirection.Descending;
    else dir = ListSortDirection.Ascending;
    gridName.Sort(gridName.SortedColumn, dir);
 }

