locked
Form DataGridView AutoSizeMode / Resize Form Width to Fit RRS feed

  • Question

  • Hi,

    I'd really like a bit of help as I've been going around in circles trying to figure this out...
    Basically I have a simple form with a 3-column datagridview.  It potentially has a varied number of rows based on the info provided (this is working fine).

    All 3 columns could vary in width potentially so I'd like to be able to setup a form where the datagridview columns are autosize-able and then the form width is adjusted accordingly on load.

    I thought I was close to getting this working but just can't seem to finish it off.  It feels like I need to return the column widths after they've been created and populated with data but when I try to do that they always return as 100 which is the default size and not the rendered size so I can't tell the form how wide it actually needs to be.

    I can't easily post a semi-working source as it pulls data in from SCCM and Active Directory so if someone could give some idea on how this might be achievable (or if it's achievable at all) in any basic fashion then that would be fantastic.

    Stuart

    Wednesday, April 17, 2019 7:29 PM

All replies

  • There is no way to auto-resize a control ad a form.  This must be done in code.

    Anchoring the grid to the form will allow the form size to control  the grid size.

    I suggest looking for articles on how to design WinForms.  These books, tutorials and articles will help you to understand what forms are and how they are designed to work in code.


    \_(ツ)_/

    Wednesday, April 17, 2019 9:50 PM
  • Here is the best example I can build to show what can be done but it is limited.

    function Show-DGVResizable{
        
        Add-Type -AssemblyName System.Windows.Forms
        $form1 = New-Object 'System.Windows.Forms.Form'
        $buttonLoad = New-Object 'System.Windows.Forms.Button'
        $buttonClear = New-Object 'System.Windows.Forms.Button'
        $datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
        $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
        
        $form1_Load = {
            $datagridview1.AutoSize = $true
        }
        
        $buttonClear_Click = {
            $datagridview1.DataSource = $null
        }
        
        $buttonLoad_Click = {
            $data = Get-ChildItem $env:windir -File | Select-Object Name, FullName, Length -First 50
            $datagridview1.DataSource = [System.Collections.ArrayList]$data
        }
        
    
        $form1.Controls.Add($buttonLoad)
        $form1.Controls.Add($buttonClear)
        $form1.Controls.Add($datagridview1)
        $form1.AutoScaleDimensions = '8, 17'
        $form1.AutoScaleMode = 'Font'
        $form1.AutoSize = $True
        $form1.AutoSizeMode = 'GrowAndShrink'
        $form1.ClientSize = '259, 349'
        $form1.Name = 'form1'
        $form1.StartPosition = 'CenterScreen'
        $form1.Text = 'Form'
        $form1.add_Load($form1_Load)
        #
        # buttonLoad
        #
        $buttonLoad.Anchor = 'Bottom, Left'
        $buttonLoad.Location = '13, 306'
        $buttonLoad.Margin = '4, 4, 4, 4'
        $buttonLoad.Name = 'buttonLoad'
        $buttonLoad.Size = '100, 30'
        $buttonLoad.TabIndex = 2
        $buttonLoad.Text = 'Load'
        $buttonLoad.UseCompatibleTextRendering = $True
        $buttonLoad.UseVisualStyleBackColor = $True
        $buttonLoad.add_Click($buttonLoad_Click)
        #
        # buttonClear
        #
        $buttonClear.Anchor = 'Bottom, Left'
        $buttonClear.Location = '146, 306'
        $buttonClear.Margin = '4, 4, 4, 4'
        $buttonClear.Name = 'buttonClear'
        $buttonClear.Size = '100, 30'
        $buttonClear.TabIndex = 1
        $buttonClear.Text = 'Clear'
        $buttonClear.UseCompatibleTextRendering = $True
        $buttonClear.UseVisualStyleBackColor = $True
        $buttonClear.add_Click($buttonClear_Click)
        #
        # datagridview1
        #
        $datagridview1.Anchor = 'Top, Bottom, Left'
        $datagridview1.AutoSizeColumnsMode = 'AllCells'
        $datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
        $datagridview1.Location = '13, 13'
        $datagridview1.Margin = '4, 4, 4, 4'
        $datagridview1.Name = 'datagridview1'
        $datagridview1.RowTemplate.Height = 24
        $datagridview1.Size = '233, 253'
        $datagridview1.TabIndex = 0
        
        $form1.ShowDialog()
        
    }
    Show-DGVResizable 
    


    \_(ツ)_/

    Wednesday, April 17, 2019 10:21 PM