简体   繁体   中英

How to resize form to fit DataGridView?

I have a form that contains dataGridView, whose coloumn are set to

dgrv1.Width =dgrv1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)+20;

I want to make the form to automaticaly follow the width of dataGridView...

Also, on maximized, I would like it to grow in height only.

Any sugestions?

Have you tried using the main form's OnChange event?

    private void MainForm_SizeChanged(object sender, EventArgs e)
    {
        this.Width = ...;
        this.Height = ...;
    }

Well, you have got a Width value. Set both the MaximumSize and MinimuSize properties of the Form to that value. Maybe with a little margin in between.

Leave the Max/Min Height properties on 0 for default.

Additional:

this gives me a Form that can only be 200 width, but the default '0' for height seems not to work.

    private void Form1_Load(object sender, EventArgs e)
    {
        this.MinimumSize = new Size(200, 400);
        this.MaximumSize = new Size(200, 1200);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM