Link to home
Start Free TrialLog in
Avatar of sbarij
sbarijFlag for Switzerland

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:

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()

Open in new window


The form loads as this:

User generated image
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:

User generated image
Can anyone help me Display the DataGridView correctly when it loads for the first time?
Avatar of Sajen Jose
Sajen Jose
Flag of India image

hi Sbarij

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.Columns[0], [System.ComponentModel.ListSortDirection]::Ascending)

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.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members 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.