Powershell - DataViewGrid - 列自动调整大小

时间:2013-08-31 23:25:58

标签: powershell datagrid autosize

对于.NET和PowerShell,我是一个完整的新手,并且想知道你们是否可以提供帮助。我正在从表单上的.CSV生成数据网格,并希望网格相应地自动调整列的大小。此外,如果我可以锁定用户调整的列/行,这将是惊人的。

Clear-Host
Function Populate-CycleCountDataGrid {
  $InventoryListArray = New-Object System.Collections.ArrayList
  $Script:InventoryList = @(Import-CSV C:\File.csv | Write-Output)
  $InventoryListArray.AddRange($Script:InventoryList)
  $CycleCountDataGrid.DataSource = $InventoryListArray
}

Function GenerateForm {
  $objForm = New-Object System.Windows.Forms.Form
  $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

  $RefreshButton_Click = {
    Populate-CycleCountDataGrid
  }

    # Form Setup
    #*******************************************************************************************\
    $OnLoadForm_StateCorrection= { $objForm.WindowState = $InitialFormWindowState }

    $objForm.Text = "CycleCount"
    $objForm.Name = "CycleCount"
    $objForm.Size = New-Object System.Drawing.Size(600,480)
    $objForm.StartPosition = 0
    $objForm.AutoSize = $False
    $objForm.MinimizeBox = $False
    $objForm.MaximizeBox = $False
    $objForm.WindowState = "Normal"

    # DataGrid
    #*******************************************************************************************\
    $CycleCountDataGrid = New-Object System.Windows.Forms.DataGrid
    $CycleCountDataGrid.Location = New-Object System.Drawing.Size(0,0)
    $CycleCountDataGrid.Size = New-Object System.Drawing.Size(592,400)
    $CycleCountDataGrid.AutoSize = $False
    $CycleCountDataGrid.AllowSorting = $False
    $CycleCountDataGrid.ReadOnly = $True
    $CycleCountDataGrid.CaptionText = "Inventory List"
    $CycleCountDataGrid.HeaderFont = New-Object System.Drawing.Font("Verdana",8.25,1,3,0)
    $CycleCountDataGrid.HeaderForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
    $CycleCountDataGrid.Font = New-Object System.Drawing.Font("Verdana",8.25,[System.Drawing.FontStyle]::Bold)
    $CycleCountDataGrid.BackColor = [System.Drawing.Color]::FromArgb(255,0,160,250)
    $CycleCountDataGrid.AlternatingBackColor = [System.Drawing.Color]::FromArgb(255,133,194,255)
    $CycleCountDataGrid.Name = "CycleCountDataGrid"
    $CycleCountDataGrid.DataBindings.DefaultDataSourceUpdateMode = 0
    $objForm.Controls.Add($CycleCountDataGrid)
    #*******************************************************************************************/

    # Refresh Button
    #*******************************************************************************************\
    $RefreshButton = New-Object System.Windows.Forms.Button
    $RefreshButton.Location = New-Object System.Drawing.Size(0,400)
    $RefreshButton.Size = New-Object System.Drawing.Size(590,45)
    $RefreshButton.Name = "RefreshButton"
    $RefreshButton.Text = "Refresh"
    $RefreshButton.UseVisualStyleBackColor = $True
    $RefreshButton.add_Click($RefreshButton_Click)
    $RefreshButton.DataBindings.DefaultDataSourceUpdateMode = 0
    $objForm.Controls.Add($RefreshButton)
    #*******************************************************************************************/

    $objForm.Topmost = $True
    $objForm.Add_Shown({$objForm.Activate()})
    $objForm.FormBorderStyle = 'Fixed3D'
    $objForm.MaximizeBox = $False
    $objForm.Add_FormClosing([System.Windows.Forms.FormClosingEventHandler]{
        if ($objForm.DialogResult -eq "Cancel") {}
    })

    $InitialFormWindowState = $objForm.WindowState
    $objForm.add_Load($OnLoadForm_StateCorrection)
    $objForm.ShowDialog()
    #*******************************************************************************************/
}
GenerateForm

3 个答案:

答案 0 :(得分:1)

添加以下代码:

$CycleCountDataGrid.Columns | Foreach-Object{
    $_.AutoSizeMode = [System.Windows.Forms.DataGridViewAutoSizeColumnMode]::AllCells
}

答案 1 :(得分:0)

将您的控件更改为system.windows.forms.datagridview,而不仅仅是数据网格。然后您就可以访问$CycleCountDataGrid.columns

每列都有一个width属性。上面的答案将尝试自动调整每个列,但如果您愿意,可以指定每个列。

$CycleCountDatarid.columns[0].width = 200

100是默认

答案 2 :(得分:0)

自动调整Windows.Forms.Datagrid的秘诀在于它有一个私有方法' ColAutoResize'你可以使用Reflection调用:

Function AutoResizeColumns([System.Windows.Forms.DataGrid] $dg1){ 
    [System.Reflection.BindingFlags] $F = 'static','nonpublic','instance' 
    $ColAutoResizeMethod = $dg1.GetType().GetMethod('ColAutoResize', $F) 
    If($ColAutoResizeMethod) { 
        For ([int]$i = $dg1.FirstVisibleColumn; $i -lt $dg1.VisibleColumnCount; $i++){
            $ColAutoResizeMethod.Invoke($dg1, $i) | Out-Null 
        }
    }
}

拥有该功能后,您可以将其添加到DataGrid的VisibleChanged和DataSourceChanged事件中,因此绘制和刷新DataGrid将调用AutoResizeColumns:

$objForm.Controls["CycleCountDataGrid"].add_DatasourceChanged({ AutoResizeColumns $objForm.Controls["CycleCountDataGrid"] } )
$objForm.Controls["CycleCountDataGrid"].add_VisibleChanged({ AutoResizeColumns $objForm.Controls["CycleCountDataGrid"] } )
$objForm.ShowDialog() | Out-Null

这可能是一种更干净的方式,但它为我工作。