2
votes

When I place a data grid view on a form, with Dock = DockStyle.Fill, I can shrink the form down to less than one row remaining before there are any issues with the data grid view scroll bar. However if I place it inside a table layout panel that is in turn docked to the form, the data grid view starts to act as though it has a minimum height, cutting off the bottom rows and bottom of the scroll bar.

Form after Resizing

    public Form1()
    {
        InitializeComponent();

        BindingList<Widget> list = new BindingList<Widget>();
        dataGridView1.DataSource = list;
        for (int i = 0; i < 100; i++)
        {
            list.Add(new Widget { MyProperty = i });
        }
    }

    private void InitializeComponent()
    {
        this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.tableLayoutPanel1.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // tableLayoutPanel1
        // 
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
        this.tableLayoutPanel1.Controls.Add(this.dataGridView1, 0, 1);
        this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.tableLayoutPanel1.RowCount = 2;
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
        // 
        // dataGridView1
        // 
        this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
        // 
        // Form1
        // 
        this.ClientSize = new System.Drawing.Size(976, 518);
        this.Controls.Add(this.tableLayoutPanel1);
        this.tableLayoutPanel1.ResumeLayout(false);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);
    }

I tried adding this.tableLayoutPanel1.AutoScroll = true; which showed it was the data grid that was not resizing, the panel scroll bar was correctly placed and not cut off.

Similar questions without an answer that worked for me:

2
Can you show a screenshot of this error? I copied your exact code and it appears to work just fine. - OhBeWise
You have to resize the window to see the problem. - Denise Skidmore
Ah, I see. I missed that. Time to debug. - OhBeWise
That's just the starting size. ClientSize changes dynamically when you resize the window. (Checked by adding handler for form Resize event.) - Denise Skidmore

2 Answers

2
votes

Add this line in InitializeComponent and it should allow the grid to shrink to nothing, letting the scrollbars show as intended.

this.dataGridView1.Size = new System.Drawing.Size(0, 0);
1
votes

I solved this by wrapping the datagridview inside a panel before putting it in the tablelayoutpanel.

Strangely this doesn't happen to all the datagridviews that I put into tablelayoutpanels.