Dan Tao
Dan Tao

Reputation: 128417

Scrolling PAST the bottom row of a DataGridView

Is there some property or method to set the vertical scroll position of a DataGridView past the bottom row?

What I mean is, say I have a DataGridView with enough rows to fill its client area. Then (by default, at least) I cannot scroll down past this point:

DataGridView scrolled to bottom row

I want to be able to force the control to continue scrolling, so that I can display some gray area below the bottom row. What I'm after would hypothetically look like this:

DataGridView scrolled beyond bottom row

Any ideas?

Upvotes: 2

Views: 822

Answers (2)

Dima Zorin
Dima Zorin

Reputation: 80

How about to use Panel with desired size and with large DataGridView inside, and move internal stuff with own ScrollBar?

DataGridView may be resized by formula:

dataGridView1.Height =
    // Total height of all rows
    dataGridView1.Rows.GetRowsHeight(DataGridViewElementStates.None) +
    // Panel visible height
    panel1.ClientSize.Height;

Full example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns.Add("Test1", "Test1");
            dataGridView1.Columns.Add("Test2", "Test2");

            dataGridView1.Rows.Add("abc", "10");
            dataGridView1.Rows.Add("def", "20");
            dataGridView1.Rows.Add("ghi", "30");
            dataGridView1.Rows.Add("jkl", "40");
            dataGridView1.Rows.Add("mno", "50");
            dataGridView1.Rows.Add("pqr", "60");
            dataGridView1.Rows.Add("stu", "70");
            dataGridView1.Rows.Add("vwx", "80");
            dataGridView1.Rows.Add("yza", "90");

            UpdatePanel();
        }

        void UpdatePanel()
        {
            dataGridView1.Height = dataGridView1.Rows.GetRowsHeight(DataGridViewElementStates.None) + panel1.ClientSize.Height;
            vScrollBar1.Minimum = 0;
            vScrollBar1.Maximum = dataGridView1.Height - panel1.ClientSize.Height;
        }

        private void panel1_Resize(object sender, EventArgs e)
        {
            UpdatePanel();
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            dataGridView1.Top = -vScrollBar1.Value;
        }
    }
}

Example form

Alternative and more easy way: set panel property AutoScroll to true, but that solution have some scrolling bugs (resets position when focus takes back). May be that bug have some workaround.

Upvotes: 1

giodamelio
giodamelio

Reputation: 5605

As far as i know there is no way of doing that short of rolling out your own custom command to display the data.

Upvotes: 2

Related Questions