Solved

Autosizing a Form based on DataGridView size?

Posted on 2014-01-28
1
659 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

10 Experts available now in Live!

Get 1:1 Help Now