sbarij
asked on
PowerShell Windows Forms DatagridView blank
Hi Experts
Am having a small issue with PowerShell when using a DatagridView in Windows Forms.
The DatagridView is being added to the form, but when the form is shown for the first time, it is "blank". Or better said, the DatagridView is a White box.
Here is the code I use:
The form loads as this:
The DatagridView is shown properly when I either click into a field of the DatagridView or when I press Tab. Once I have done this, the Datagrid is displayed correctly:
Can anyone help me Display the DataGridView correctly when it loads for the first time?
Am having a small issue with PowerShell when using a DatagridView in Windows Forms.
The DatagridView is being added to the form, but when the form is shown for the first time, it is "blank". Or better said, the DatagridView is a White box.
Here is the code I use:
Add-Type -AssemblyName System.Windows.Forms
$objFrm = New-Object System.Windows.Forms.Form
$dgv_Info = New-Object System.Windows.Forms.DataGridView
$objFrm.Size = New-Object System.Drawing.Size(722, 563)
$objFrm.MinimumSize = New-Object System.Drawing.Size(722, 563)
$dgv_Info.Location = New-Object System.Drawing.Point(13, 342)
$dgv_Info.Size = New-Object System.Drawing.Point(681, 145)
$dgv_Info.Anchor = $AnchorStyle_BottomLeftRight
$dgv_Info.ColumnHeadersVisible = $true
$dgv_Info.RowHeadersVisible = $true
$dgv_cbCellSQLInst = New-Object System.Windows.Forms.DataGridViewComboBoxCell
$dgv_cbCellSQLInst.DataSource = $SQLInstanceNumbers
$outvar = $dgv_Info.Columns.Add($dgv_cbCellSQLInst)
$dgv_Info.Columns[0].Name = "Header 1"
$dgv_Info.Columns[0].AutoSizeMode = 'Fill'
$dgv_Info.Sort($dgv_Info.Columns[0], [System.ComponentModel.ListSortDirection]::Ascending)
$dgv_cbCellDrv = New-Object System.Windows.Forms.DataGridViewComboBoxCell
$dgv_cbCellDrv.DataSource = $DrvLetters
$outvar = $dgv_Info.Columns.Add($dgv_cbCellDrv)
$dgv_Info.Columns[1].Name = "Header 2"
$dgv_Info.Columns[1].AutoSizeMode = 'Fill'
$dgv_DBDisk = New-Object System.Windows.Forms.DataGridViewTextBoxCell
$outvar = $dgv_Info.Columns.Add($dgv_DBDisk)
$dgv_Info.Columns[2].Name = "Header 3"
$dgv_Info.Columns[2].AutoSizeMode = 'Fill'
$dgv_LogDisk = New-Object System.Windows.Forms.DataGridViewTextBoxCell
$outvar = $dgv_Info.Columns.Add($dgv_LogDisk)
$dgv_Info.Columns[3].Name = "Header 4"
$dgv_Info.Columns[3].AutoSizeMode = 'Fill'
$dgv_TempDisk = New-Object System.Windows.Forms.DataGridViewTextBoxCell
$outvar = $dgv_Info.Columns.Add($dgv_TempDisk)
$dgv_Info.Columns[4].Name = "Header 5"
$dgv_Info.Columns[4].AutoSizeMode = 'Fill'
$dgv_BckpDisk = New-Object System.Windows.Forms.DataGridViewTextBoxCell
$outvar = $dgv_Info.Columns.Add($dgv_BckpDisk)
$dgv_Info.Columns[5].Name = "Header 6"
$dgv_Info.Columns[5].AutoSizeMode = 'Fill'
$dgv_Info.add_CellValueChanged($dgv_Info_CellValuechanged)
$dgv_Info.add_UserDeletedRow($dgv_Info_UserDeletedRow)
$objFrm.Controls.Add($dgv_Info)
$objFrm.ShowDialog()
The form loads as this:
The DatagridView is shown properly when I either click into a field of the DatagridView or when I press Tab. Once I have done this, the Datagrid is displayed correctly:
Can anyone help me Display the DataGridView correctly when it loads for the first time?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
I don't know if this is still an issue, but you may want to comment out this line (or rather investigate a bit more) :)
$dgv_Info.Sort($dgv_Info.C
I have a feeling the sorting activity is causing some issues with the selection, if you remove or comment out this line it causes the display to come out fine.