如何自动设置表单和datagridview宽度csv
How to automatically set form and datagridview width csv
我有一个 Powershell 脚本,我已从 Internet 下载并根据我的目的进行了修改。我的目的是打开 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()
我们可以让您的 UI 自动调整大小,但我们需要删除硬编码的 size
值并将其转换为 .MinimumSize
。您可能还想添加 .MaximumSize
属性,否则有人可能会打开一个巨大的 .csv
文件并得到一个非常难看的文件 UI。
我们还想将 .AutoScale
和 .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's Header Height property, like in this article.
我有一个 Powershell 脚本,我已从 Internet 下载并根据我的目的进行了修改。我的目的是打开 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()
我们可以让您的 UI 自动调整大小,但我们需要删除硬编码的 size
值并将其转换为 .MinimumSize
。您可能还想添加 .MaximumSize
属性,否则有人可能会打开一个巨大的 .csv
文件并得到一个非常难看的文件 UI。
我们还想将 .AutoScale
和 .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's Header Height property, like in this article.