Datagridview vertical scrollbar

How do i force the Datagridview vertical scrollbar always to show ?

Thanks
LVL 18
vbturboAsked:
Who is Participating?
 
REA_ANDREWConnect With a Mentor Commented:
Hi,

Apologies I had a few goes at this, and when I said you cannot do this, I was clearly wrong.  Inffact if you did what i said and added a vertical scroll bar then the datagridview has 2 of them, this is inefficient.  What you need to do is create a dervived object which inherits from the DataGridView.  I am a C# man myself but I don't think it will be too hard to transpose this:

You may need to tweak sizing behaviour also, so if the data gridview resizes, adjust the scroll bar hight if it does not have any rows to scroll.

Cheers

Andrew
    public partial class CustomDataGridView : DataGridView
    {
        public CustomDataGridView()
        {
            InitializeComponent();
            this.VerticalScrollBar.Visible = true;
            this.VerticalScrollBar.VisibleChanged += new EventHandler(VerticalScrollBar_VisibleChanged);
            this.VerticalScrollBar.SetBounds(this.VerticalScrollBar.Location.X, this.VerticalScrollBar.Location.Y, this.VerticalScrollBar.Width, this.Height);
        }
 
        void VerticalScrollBar_VisibleChanged(object sender, EventArgs e)
        {
            this.VerticalScrollBar.Visible = true;
        }
 
    }

Open in new window

0
 
REA_ANDREWCommented:
You cannot, you would have to implement a Vertical Scroll Bar Control and link up the datagridview binding events so that it can correctly scroll it.

Andrew
0
 
vbturboAuthor Commented:
do you have a small sample code on how to do that ?

Jens
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
vbturboAuthor Commented:
i have about 30 rows in my grid (win app)
The height of the grid is set so that it displays 5 rows and the scrollbar property is set to vertical but it never shows
0
 
SanclerCommented:
Jens

Are you wanting the vscrollbar ALWAYS to show?  Or is your problem that it is not showing when there are sufficient records to make it necessary?

If it's the latter, and the .Scrollbars prperty is set to Vertical, then there's something odd going on.  Are you setting the .Scrollbars property at desgin time, or run time?  How do you set the height to show precisely 5 records.  At the moment, I've no ideas, and cannot reproduce the problem, but those look to me to be factors that might be relevant.  What happens if you change the .Scrllbars property to Both?

Roger

PS - off topic

As you're about, if you're not too busy, you might like to pop in here

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23133003.html

I think there's a database design problem needs sorting out first, but it looks like it might then move into pukka DataRelations ;-)
0
 
vbturboAuthor Commented:
Roger

Thanks for popping in - i've already looked at your link and its looking very interresting-;)

This is how i populate the grid ,selecting only the specific columns i need.
I did set the scrollbar property (in the grids properties) to vertical but it doesnt show when populating the grid this way....hmmm
and  there are more rows than the grid can show (due to the height of the grid)

i have set scrollbar property in design time and also tried to apply it in runtime also, but.....no luck.
I dont really care if it is going to be a some sort of permanent scrollbar solution i just need the scrollbar to
to show when there are more rows than that can viewed in the grid.


        'Kreditnota kopi sendt grid
        dgGrid.AutoGenerateColumns = False
        dgGrid.AutoSize = True
        dgGrid.Columns(0).DataPropertyName = ds.Tables(0).Columns("navn").ColumnName()
        dgGrid.Columns(1).DataPropertyName = ds.Tables(0).Columns("dato").ColumnName()
        Dim dvKreditNotaSendt As New DataView(ds.Tables(0))
        dvKreditNotaSendt.RowFilter = String.Format("status = '{0}'", "Kredit kopi sendt")
        dgGrid.DataSource = dvKreditNotaSendt

hope you got a clue-:)

Jens
0
 
vbturboAuthor Commented:
Thanks Andrew

Ill give it a try tomorrow at work and then drop a post , Its becoming late in Denmark.

Jens
0
 
SanclerConnect With a Mentor Commented:
Jens

This is the offending line

        dgGrid.AutoSize = True

I'm not sure what it's supposed to do.  So far as I can see from the docs: nothing.  But if you get rid of it, the vertical scrollbar should show when the number of records makes it appropriate.

Roger
0
 
vbturboAuthor Commented:
Thanks Roger , nothing escapes the haweyes from Wales (just did the trick as desired).

Andrew
i've tested your solution also (and it works just fine)

Well here comes the hard part (i'm not able to accept both solutions) which both of you deserve.

Jens
0
All Courses

From novice to tech pro — start learning today.