Solved

Autosizing a Form based on DataGridView size?

Posted on 2014-01-28
1
668 Views
Last Modified: 2014-01-29
I have a simple form that contains a Label, DataGridView and a Panel and within the Panel, a Label and ComboBox.

What I'm trying to do is autosize the form and not sure how to calculate the dimensions of the DGV. Here is my code.

    Private Sub CalculateControlPositions()
        Try
            lblItemInfo.Top = 0
            lblItemInfo.Left = 0
            lblItemInfo.Width = Me.Width
            lblItemInfo.Height = 28

            pnl.Top = lblItemInfo.Height + dgvSpecs.Height
            pnl.Left = 0

            dgvSpecs.Top = lblItemInfo.Height

            rowHght = dgvSpecs.RowCount * dgvSpecs.RowTemplate.Height

            Me.Size = New Size(dgvSpecs.Width + 26, rowHght + pnl.Height + 26)
            Me.ClientSize = New Size(dgvSpecs.Width, rowHght + pnl.Height)

            EH.ErrorMessage = ""

        Catch ex As Exception
            EH.ErrorMessage = "CalculateControlPositions() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(frmMain, frmMain.sbr, EH.ErrorMessage)
    End Sub

Open in new window

0
Comment
Question by:BlakeMcKenna
1 Comment
 
LVL 14

Accepted Solution

by:
Matti earned 500 total points
ID: 39817492
The event is a bit problem, you found some event from the grid or just use the code that updates it.

grid.left = 0
Me.height = Grid.height
Me.width = Grid.width

Of cause if you need to fit this perfect you need calculate all borders and bar sizes from API call's and use them to minus some of the grid size, you can estimate this, but it's not good in all operating system settings.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now