Denise Skidmore
Denise Skidmore

Reputation: 2416

DataGridView acts as though it has a minimum size inside a TableLayoutPanel

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:

Upvotes: 2

Views: 1778

Answers (2)

Shao Skywalker
Shao Skywalker

Reputation: 21

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.

Upvotes: 1

OhBeWise
OhBeWise

Reputation: 5454

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

Upvotes: 2

Related Questions