Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2263
  • Last Modified:

How do I autosize my .NET windows form so my datagridview displays correct

I have a datagridview that I fill on page load in my VB.NET windows forms application.  The cells in the datagrid are set to autosize allcells.  I now want the windows form that holds the datagridview to autosize based on the size of the grid.  I set the windows form autosize = true but it is not changing size.

What am I missing?
0
handyjay
Asked:
handyjay
  • 3
  • 2
1 Solution
 
Todd GerbertIT ConsultantCommented:
Does setting the Dock property of the data grid to DockStyle.Fill have the desired result?
0
 
handyjayAuthor Commented:
no that does not work
0
 
Todd GerbertIT ConsultantCommented:
Is the DataGridView the only thing on the form?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
handyjayAuthor Commented:
yes
0
 
Todd GerbertIT ConsultantCommented:
You can handle the Paint and Resize events of the DataGridView, and set the form to the same height/width of the DataGridView. e.g.,


Private Sub DataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
    Me.Size = Me.DataGridView1.Size
End Sub
 
Private Sub DataGridView1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Resize
    Me.Size = Me.DataGridView1.Size
End Sub

Open in new window

0
 
JulienVanCommented:
Hi, I've just tried it and it works:
 - Set you're form AutoSize property to true
 - Copy following ExpandDgv method in your form code
 - In the Shown event of the form, call the method
        private void ExpandDgv(DataGridView dgv)
        {
            int size = 0;
            foreach (DataGridViewColumn col in dgv.Columns)
                size += col.Width;
            if (dgv.RowHeadersVisible) size += dgv.RowHeadersWidth;
            dgv.Width = size;
        }
 
        private void Form1_Shown(object sender, EventArgs e)
        {
            ExpandDgv(dataGridView1);
        }

Open in new window

0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now