views:

2317

answers:

2

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 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 accieve? And if so, how do I get the vertical scrollbar to allways be visible?

+1  A: 

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. Leave me a comment if you need help on the subclassing.

gkrogers
I do need help on subclassing :) This is all very new to me...
Sakkle
+2  A: 

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;
        }
     }
gkrogers
Ok... So I understand what you're doing here, however I have allready tried setting the scrollbar visible in the DataGridView. Why should this make it behave any differently?
Sakkle
Because you're overriding the VisibleChanged event. If you set the scrollbar visible on load, but then it resizes itself and decides it doesn't need a vertical scrollbar after all, it will disappear, but this event will fire, whereupon you override the property and (hopefully) voila!
gkrogers
Ahaaaaa... thank you :)
Sakkle
Y'welcome! I've had a bit of a play and it does seem to work, remaining visible but greyed out when it's not needed, but it looks like you might have to set the scrollbar's height yourself.
gkrogers