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

0

コントロールを単なるデータグリッドではなく、system.windows.forms.datagridview に変更します。次に、アクセスできます$CycleCountDataGrid.columns

各列には width プロパティがあります。上記の回答は各列の自動サイズ変更を試みますが、必要に応じて各列を指定できます。

$CycleCountDatarid.columns[0].width = 200

100 がデフォルトです

于 2016-10-28T20:52:32.990 に答える