0

当谈到 .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
4

3 回答 3

1

添加以下代码:

$CycleCountDataGrid.Columns | Foreach-Object{
    $_.AutoSizeMode = [System.Windows.Forms.DataGridViewAutoSizeColumnMode]::AllCells
}
于 2013-09-01T10:54:47.380 回答
0

自动调整 Windows.Forms.Datagrid 的秘诀在于它有一个私有方法“ColAutoResize”,您可以使用反射调用:

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

可能有一种更清洁的方法可以做到这一点,但它对我有用。

于 2018-01-24T00:55:49.823 回答
0

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

每列都有一个宽度属性。上面的答案将尝试自动调整每一列的大小,但您可以根据需要指定每一列。

$CycleCountDatarid.columns[0].width = 200

100 是默认值

于 2016-10-28T20:52:32.990 回答