Datagridview导出HeaderText

时间:2017-04-19 18:22:58

标签: winforms powershell datagridview

我正在使用下面的datagridview。我正在尝试导出标题名称。

如果你运行这个代码并点击导出,它只会输出用户最初可以看到的列标题(最多为'Model'),但它不会输出'Version'或'Last Rebooted'。如果在按下导出之前滚动到右侧,则它将显示所有列名称。

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(1040,518)
$form.KeyPreview = $true
$form.StartPosition = ‘centerscreen’
$form.BackColor = 'MidnightBlue'
$form.Add_KeyDown({if($_.KeyCode -eq "Escape"){$form.Close()}})
$form.Text = "Dialog Box 3.4" 
$form.Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell_ise.exe")
$form.MinimumSize = New-Object System.Drawing.Size(1040,518)

$buttonPanel4 = New-Object Windows.Forms.Panel
$buttonPanel4.Size = New-Object Drawing.Size @(290,70)
$buttonPanel4.Dock = "left"
$buttonPanel4.BackColor = 'MidnightBlue'

$DataGrid = New-Object System.Windows.Forms.DataGridView
$DataGrid.Location = New-Object System.Drawing.Size(298,29)
$DataGrid.Dock = "Fill"
$DataGrid.BorderStyle = ‘FixedSingle’
$DataGrid.ColumnHeadersDefaultCellStyle.Font = New-Object System.Drawing.Font(“segoe UI”,9.25)
$DataGrid.DefaultCellStyle.Font = New-Object System.Drawing.Font(“segoe UI”,9.25)
$DataGrid.AllowUserToAddRows = $false
$DataGrid.RowHeadersVisible = $false
$DataGrid.BackgroundColor = "White"

$DataGrid.ColumnCount = 10
$DataGrid.Columns[0].Name = ‘Machine’
$DataGrid.Columns[1].Name = ‘OperatingSystem’
$DataGrid.Columns[2].Name = ‘ServicePack’
$DataGrid.Columns[3].Name = ‘Architecture’
$DataGrid.Columns[4].Name = ‘Domain’
$DataGrid.Columns[5].Name = ‘PhysicalMemory’
$DataGrid.Columns[6].Name = ‘Manufacturer’
$DataGrid.Columns[7].Name = ‘Model’
$DataGrid.Columns[8].Name = ‘Version’
$DataGrid.Columns[9].Name = ‘Last Rebooted’
$DataGrid.Columns[9].Width = '140'

$Exportbutton = New-Object System.Windows.Forms.Button
$Exportbutton.Location = New-Object System.Drawing.Size(9,350)
$Exportbutton.Size = New-Object System.Drawing.Size(85,23)
$Exportbutton.Text = “Export-CSV”
$Exportbutton.BackColor = ‘LightGray’
$Exportbutton.UseVisualStyleBackColor = $true
$Exportbutton.Font = New-Object System.Drawing.Font(“segoe UI”,9)
$Exportbutton.Add_Click({
    $columnNames = $null
    $columnNames = $DataGrid.Columns[0].HeaderText
    for($i = 1; $i -lt $DataGrid.ColumnCount;$i++){
        $columnNames += ",$($DataGrid.Columns[$i].HeaderText)"
        write-host $($DataGrid.Columns[$i].HeaderText) -ForegroundColor Magenta
    }
    write-host $columnNames -foregroundcolor cyan
})

$buttonPanel4.Controls.Add($Exportbutton)
$form.Controls.Add($DataGrid)
$form.Controls.Add($buttonPanel4)
$form.ShowDialog() | out-null

是否有这种情况发生的原因以及如何导出所有列名而不先向右滚动?

1 个答案:

答案 0 :(得分:0)

正如@pandemic指出的那样,$buttonPanel4未定义/显示,而且您似乎将DataGrid.Dock属性设置为Fill,如果该面板已存在,则会覆盖此按钮

我没有答案为什么header文本返回一个空字符串,直到标题在数据网格中变得可见。我猜这是因为HeaderText属性尚未设置。我假设如果HeaderText s属性尚未设置,则默认情况下将为Name列属性(如果需要)。如果您使用HeaderText属性设置每个列Name属性,则不会发生此问题。

因此,请设置每个列HeaderText或使用其Name属性,如下面的代码所示。

我更改了下面的代码,使用列名而不是标题文本,它似乎正常工作。另外,我更改了逗号插入$columnNames字符串变量的方式,以便在索引0处开始循环。希望这会有所帮助。

$Exportbutton.Add_Click({
   $columnNames = $null
   for($i = 0; $i -lt $DataGrid.ColumnCount;$i++){
     $columnNames += "$($DataGrid.Columns[$i].Name)"
     if ($i -lt $DataGrid.ColumnCount - 1) {
       $columnNames += ","
     }
     write-host $($DataGrid.Columns[$i].Name) -ForegroundColor Magenta
   }
   write-host $columnNames -foregroundcolor cyan
})