我正在使用下面的datagridview。我試圖導出標題名稱。Datagridview Export HeaderText
如果您運行此代碼並點擊導出,它將只輸出用戶最初可以看到的列標題(最多爲'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
是否有這種情況發生的原因,以及如何導出所有列名而不滾動到正確的第一個?
什麼是變量'$ buttonPanel4'? – pandemic
對不起,我把這個從我的更大的程序中拼出來,忘了包含面板。它在左邊,數據網格在右邊。 – CuriousOne