none
Form size to follow DataGrid size up to max size for form

    Question

  • Greetings:

    My application displays a DataGridView control inside a form. I want the DataGridView to have a minimum size but no particular maximum.

    The surrounding form, however, must be the size of the DataGridView control, plus whatever necessary overhead, but allowed to grow no greater than some arbitrary value. In this fashion the form would grow to a maximum size and the DataGridView would scroll if it cannot all fit inside the form.

    What is the correct way of setting the DataGridView to grow to a maximum size and the form to grow with the size of the DataGridView up to some maximum size?

    What about the issue of the vertical scrollbars? If the data in the DataGridView is short then a blank space appears between the last column and the right-hand side of the form. How can the form be adjusted to take the absence of the vertical scrollbars into consideration?

    Best regards,
    Randyl Kent Plampin


    Just trying to do my job

    Friday, July 12, 2013 12:03 PM

All replies

  • Randyl,

    You might just dock it (fill if possible, but to the top or bottom if not) then manually adjust your form's size.

    However, you're not taking into account the fact that different users will have different screen resolutions, so what you see may or may not be what they see, but by docking it, it will fill whatever area is available.

    I'm not sure that's what you're looking for but I thought I'd drop this in anyway.


    Please call me Frank :)

    Friday, July 12, 2013 12:08 PM
  • Hello,

    Adding to Frank's reply, you might consider working with MinimumSize of the form taking into consideration other controls to calculate the min size of the form coupling this with docking the DataGridView and variable screen resolution and font sizes as per Frank. There is no magic answer but thoughts on docling and MinimumSize working to the lowest screen resolution you would consider a customer using.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, July 12, 2013 12:56 PM
    Moderator
  • Hi

    Just to add to the replies from Frank and Kevin :)

    Here is some code that may illustrate some of the features you want.
    ' new project with default BLANK Form1
    ' replace all Form1 code with this code
    Option Strict On
    Option Infer Off
    Public Class Form1
        Private DGV As New DataGridView
        Private minFormWidth As Integer = 900
        Private minFormHeight As Integer = 700
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Me.MinimumSize = New Size(minFormWidth, minFormHeight)
    
            DGV.Location = New Point(0, 0)
            DGV.Size = New Size(Me.Width - 100, Me.Height - 100)
            DGV.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Bottom
    
            For c As Integer = 1 To 4
                DGV.Columns.Add("Column" & c.ToString, "Column" & c.ToString)
            Next
            Me.Controls.Add(DGV)
    
            ' dummy right side Overhead, using buttons
            For i As Integer = 1 To 3
                Dim b As New Button
                b.Text = "B" & i.ToString
                b.Name = "Button" & i.ToString
                b.Width = 60
                b.Location = New Point(DGV.Right + 8, i * 33)
                b.Anchor = AnchorStyles.Top Or AnchorStyles.Right
                AddHandler b.Click, AddressOf Button_Click
                Me.Controls.Add(b)
            Next
    
            ' dummy bottom Overhead, using panel
            Dim p As New Panel
            p.Width = DGV.Width - 20
            p.Location = New Point(10, DGV.Bottom + 6)
            p.Height = 50
            p.BackColor = Color.LightGray
            p.BorderStyle = BorderStyle.FixedSingle
            p.Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Bottom
            Me.Controls.Add(p)
        End Sub
        Private Sub Button_Click(sender As System.Object, e As System.EventArgs)
            Dim b As Button = DirectCast(sender, Button)
            If b.Name = "Button2" Then
                Dim r As New Random
                For i As Integer = 1 To 100
                    DGV.Rows.Add("Row " & i.ToString, r.Next(100, 999).ToString, r.Next(1000, 9999).ToString, r.Next(-9999, 0))
                Next
                MessageBox.Show("You filled the DGV grid")
            Else
                MessageBox.Show("You clicked " & b.Text)
            End If
    
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, July 12, 2013 1:30 PM go away
    Friday, July 12, 2013 1:30 PM
  • Greetings:

    Thank you Frank, Kevin and Les.

    I cannot find any combination of MinimumSize and Docking that provides any relief. Screen resolution issues will just have to wait until the application is actually running.

    The sample code from Mr. Livingston also provides no relief for the reason that the DGV control in my application is stuck on his MinimumSize and will not expand to fill whatever size he should end up as. Yes, I can set the size of the DGV as in his example, but how do I know what size that should be? If the number of rows is short and I set DGV's size to the maximum form size then there is the "blank" space between the last row and the edge of the form. This what I was trying to avoid.

    So, just to be clear: As of right now the first issue is to get the DGV to set his height to however many rows he has. For some odd and strange reason I thought that this was automatic; apparently this is not the case. A thought all of a sudden occurred to me: Perhaps a fixed DGV grid size is what is intended. Perhaps this behavior is by design.

    Should I just set both the DGV and the form to a particular size and be done with it, and forget the "blank" space if the number of rows does not fill the grid?

    Best regards,

    Randyl Kent Plampin


    Just trying to do my job

    Friday, July 12, 2013 8:52 PM
  • Hi

    And who is Mr Livingston?  :)


    Regards Les, Livingston, Scotland

    Friday, July 12, 2013 8:57 PM
  • Randyl,

    To fix that "blank space on the right", set the last column to automatically fill:

    .Columns(5).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill


    There I'm assuming the column 5 is the last column, but you get the idea.

    Please call me Frank :)

    Friday, July 12, 2013 8:59 PM
  • Greetings:

    "To fix that "blank space on the right", set the last column to automatically fill:"

    You bet. That worked nicely. Thank you very much, Frank. I had it set as "AllCells" and so when the DGV only had a few rows and did not take up the entire vertical space the scrollbars disappeared and the "blank space" appeared in its place. The "blank space" is now gone.

    I would be happy to mark your response as "Mark as answer" if I am allowed to mark the future one that resolves the issue of getting my form to fit snug around the vertically-short DGV.

    As of right now filling the DGV with many rows presents no problems and the scrollbars are present. The only outstanding issue is how to get the form to shrink to the size of the DGV, plus the overhead, down to a minimum size.

    Best regards,

    Randyl Kent Plampin


    Just trying to do my job

    Saturday, July 13, 2013 6:29 AM
  • Hi

    You can mark as answer all the posts that you consider of value to getting the answer you needed. Sometimes, answers are gleaned from several posts as the questions are sometimes difficult to understand by any one answerer until the flow of posts reveals what was needed.


    Regards Les, Livingston, Scotland

    Saturday, July 13, 2013 10:53 AM
  • Greetings:

    Apparently after I set Frank's reply as one of the answers everyone stopped looking at this thread. I am going to unmark this thread in the hopes of getting an answer to the original question. I may or not be successful. If we get the answer for re-sizing the form to fit the datagrid then I will remark Frank's answer.

    Best regards,

    Randyl Kent Plampin


    Just trying to do my job

    Tuesday, July 16, 2013 5:12 AM