我有一个从 Internet 下载的 Powershell 脚本,并针对我的目的进行了修改。我的目的是打开 CSV,进行更改,点击保存按钮,然后 X 退出。
我的问题是我必须手动设置每个 CSV 的宽度(来自脚本)。我可以手动调整表格增长/缩小,但不能调整 DataGrid
我想
- Form 和 DataGrid 根据启动时或手动时的列数自动增长或缩小。
- 当我对列执行 AutoFit 时,Form 和 DataGrid 会自动增长或缩小
- 如果可以的话,我基本上不想要滚动条
我查看了一些关于 .net 类/方法等的文档,并且已经能够找出很多东西,但是这个。我确定我没有问正确的问题。任何有关资源或示例的建议将不胜感激。
我的代码在这里。
[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()