ScrollBar in DataGridView

2020-02-13 05:07发布

问题:

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.

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.

Is this at all possible to achieve? And, if so, how do I get the vertical scrollbar to always be visible?

回答1:

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.

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;
        }
     }


回答2:

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);
 }