0

我有一个从 Internet 下载的 Powershell 脚本,并针对我的目的进行了修改。我的目的是打开 CSV,进行更改,点击保存按钮,然后 X 退出。

我的问题是我必须手动设置每个 CSV 的宽度(来自脚本)。我可以手动调整表格增长/缩小,但不能调整 DataGrid

我想

  • Form 和 DataGrid 根据启动时或手动时的列数自动增长或缩小。
  • 当我对列执行 AutoFit 时,Form 和 DataGrid 会自动增长或缩小
  • 如果可以的话,我基本上不想要滚动条

我查看了一些关于 .net 类/方法等的文档,并且已经能够找出很多东西,但是这个。我确定我没有问正确的问题。任何有关资源或示例的建议将不胜感激。

这是我打开包含许多列的 csv 时的意思的示例在此处输入图像描述

或几列: 在此处输入图像描述

我的代码在这里。

[reflection.assembly]::load("System.Windows.Forms") | Out-Null
[reflection.assembly]::load("System.Drawing") | Out-Null


# This block of code is a file dialog open box
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
    # This uses Environmental Directory Settings such as Desktop
    # InitialDirectory = [Environment]::GetFolderPath('Desktop')

    # Here you can pick the default directory.  If you comment it out, 
    # then OpenFileDialog seems to go to the last directory you chose a file from
    InitialDirectory = "C:\temp\Ricoh"

    # Pick file extensions to chose from.  The first is default.
    Filter = 'Comma Separated Values (*.csv)|*.csv|All Files (*.*)|*.*|SpreadSheet (*.xlsx)|*.xlsx'
}

# The actual dialog open box
$dialogOpen = $FileBrowser.ShowDialog()

# This returns the full path and filename
$pathFileName = $FileBrowser.FileName
# Write-Host $pathFileName 

# This returns the filename only
$fileName = $FileBrowser.SafeFileName
# Write-Host $fileName 

$OnLoadForm_UpdateGrid= {

    # This returns the filename minus extension.
    $baseName = Get-Item $pathFileName | Select-Object -ExpandProperty BaseName
    # Write-Host $baseName

    $tmp = $FileBrowser.InitialDirectory + '\' + $baseName + '.tmp'
    Write-Host $tmp

    #Make a copy of the file so we can import it and leave the real file free for exporting to
    Copy-Item $pathFileName -Destination $tmp

    # Load the tempfile into memory so we can work
    $tmpFileName = Import-Csv $tmp

    #Remove the tempfile now
    Remove-Item $tmp

    #Select the datasource so we can prep for the dataGridView
    $dataGridView1.DataSource=[System.Collections.ArrayList]$tmpFileName

    $form.refresh()
}


# This button will save the file
$button1_OnClick= {    
    $dataGridView1.Rows |Select -Expand DataBoundItem | Export-Csv $pathFileName -NoType
}

$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Form Text Goes Here"
$Form.TopMost = $true

$form.KeyPreview = $true
$form.StartPosition = "centerscreen"

$dataGridView1 = New-Object System.Windows.Forms.DataGridView -Property @{
}

$sds_width = 900
$sds_height = 450

$form.Size = New-Object System.Drawing.Size($sds_width,$sds_height)

$dataGridView1.Size = New-Object System.Drawing.Size(($sds_width - 25),($sds_height - 100))
$dataGridView1.AutoResizeColumns()
$dataGridView1.AllowUserToOrderColumns = $true
$dataGridView1AllowUserToResizeColumns = $true
$dataGridView1.AutoResizeColumns()

$dataGridView1.Name = $baseName
$dataGridView1.DataMember = ""
$dataGridView1.TabIndex = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 5
$System_Drawing_Point.Y = 5

$dataGridView1.Location = $System_Drawing_Point
$form.Controls.Add($dataGridView1)
$form.add_Load($OnLoadForm_UpdateGrid)

$button = New-Object Windows.Forms.Button
$button.text = "Save"
$button.Location = New-Object Drawing.Point(5,($dataGridView1.height + 25))
$button.Size = New-Object Drawing.Point(125, 25)

$button.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right

$button.TabIndex ="1"
$button.add_Click($button1_OnClick)
$form.controls.add($button)

$form.ShowDialog()
$form.Dispose()
4

1 回答 1

1

我们可以使您的 UI 自动调整大小,但我们需要删除硬编码的size值并将其转换为.MinimumSize. 您可能还想添加.MaximumSize属性,或者有人可能会打开一个巨大的.csv文件并获得一个超级难看的 UI。

我们还希望将.AutoScaleand.AutoSize属性设置为 true,以便整体更改看起来像。

$form.MinimumSize= New-Object System.Drawing.Size($sds_width,$sds_height)
$form.AutoScale = $true
$form.AutoSize = $true

我们还需要在 上设置相同的属性DataGridView

$dataGridView1.MinimumSize = New-Object System.Drawing.Size(($sds_width - 25),($sds_height - 100))
$dataGridView1.AutoScale...

我也遇到了一个错误,因为我没有C:\temp\Ricoh路径,所以你可能想在那里进行一些错误处理,line 38比如

if (Test-Path C:\temp\Ricoh){
   Write-Host "Found C:\temp\Ricoh path"
}
else{
   New-item "C:\temp\Ricoh" -ItemType Directory 
}

现在,我并没有说它看起来很漂亮,特别是如果用户像我一样有奇怪的缩放比例。

在此处输入图像描述

您可能想要做的一件事是设置Column 的 Header Height 属性,就像在这篇文章中一样

于 2020-05-24T15:18:22.397 回答