Asked by:
Set Grid Column Width

Question
-
The following is a grid I made and need to know how to change default column width. Is there a way to do it?
function Update-Grid {
$list = New-Object System.Collections.ArrayList
$a = @()
$item = New-Object System.Object
$item | Add-Member -Type NoteProperty -Name 'Col 1' -Value 'ABC'
$item | Add-Member -Type NoteProperty -Name 'Col 2' -Value 'DEFG'
$item | Add-Member -Type NoteProperty -Name 'Col 3' -Value 12.41
$item | Add-Member -Type NoteProperty -Name 'Col 4' -Value 34.21
$item | Add-Member -Type NoteProperty -Name 'Col 5' -Value 56.77
$item | Add-Member -Type NoteProperty -Name 'Col 6' -Value 1234580
$a += $item
$list.AddRange($a)
$grid.DataSource = $list
}
$OnLoadForm_UpdateGrid={ Update-Grid }$grid = New-Object System.Windows.Forms.DataGrid
$grid.Size = New-Object System.Drawing.Size(500,550)$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(505,200)
$form.Add_Load($OnLoadForm_UpdateGrid)
$form.Controls.Add($grid)
$Form.ShowDialog()Tuesday, June 19, 2018 9:55 PM
All replies
-
$grid.Columns[0].Width = 100
Easier:
Use a DataGridView
$grid = New-Object System.Windows.Forms.DataGridView
$grid.AutoSizeColumnsMode = 'AllCells'\_(ツ)_/
Tuesday, June 19, 2018 10:14 PM -
This is what you are trying to do:
$dataGridView1_Load = { $a = @( [pscustomobject]@{ 'Col 1' = 'ABC1' 'Col 2' = 'Big Data Field' 'Long Column 3' = 12.41 'Col 4' = 34.21 'Col 5' = 56.77 'Col 6' = 1234580 } [pscustomobject]@{ 'Col 1' = 'ABC2' 'Col 2' = 'DEFG' 'Long Column 3' = 12.41 'Col 4' = 34.21 'Col 5' = 56.77 'Col 6' = 1234580 } [pscustomobject]@{ 'Col 1' = 'ABC3' 'Col 2' = 'DEFG' 'Long Column 3' = 12.41 'Col 4' = 34.21 'Col 5' = 56.77 'Col 6' = 1234580 } ) $dataGridView1.DataSource = [collections.arraylist]$a } $form = New-Object System.Windows.Forms.Form $form.Size = '505,200' $form.StartPosition = 'CenterScreen' $form.Add_Load($dataGridView1_Load) $dataGridView1 = New-Object System.Windows.Forms.DataGridView $form.Controls.Add($dataGridView1) $dataGridView1.Dock = 'Fill' $dataGridView1.AutoSizeColumnsMode = 'AllCells' $Form.ShowDialog()
\_(ツ)_/
- Edited by jrv Tuesday, June 19, 2018 10:27 PM
Tuesday, June 19, 2018 10:25 PM -
Thanks. One more question, I try to add the setting below to make the column header centered but not working. what am I wrong? Thanks
$dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = 'MiddleCenter'
Also I try to set column header color with no luck:
$dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = 'Green'
- Edited by Ian3 Wednesday, June 20, 2018 2:58 AM
Wednesday, June 20, 2018 2:37 AM -
You have to create a new style object and set its properties to change the built-in defaults.
$System_Windows_Forms_DataGridViewCellStyle_1 = New-Object 'System.Windows.Forms.DataGridViewCellStyle' $System_Windows_Forms_DataGridViewCellStyle_1.Alignment = 'MiddleCenter' $System_Windows_Forms_DataGridViewCellStyle_1.BackColor = 'Control' $System_Windows_Forms_DataGridViewCellStyle_1.Font = 'Microsoft Sans Serif, 8.25pt' $System_Windows_Forms_DataGridViewCellStyle_1.ForeColor = 'WindowText' $System_Windows_Forms_DataGridViewCellStyle_1.SelectionBackColor = 'Highlight' $System_Windows_Forms_DataGridViewCellStyle_1.SelectionForeColor = 'HighlightText' $System_Windows_Forms_DataGridViewCellStyle_1.WrapMode = 'True' $datagridview1.ColumnHeadersDefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_1
\_(ツ)_/
Wednesday, June 20, 2018 3:14 AM -
nope, the header text still not centered after adding these codes
and the header cell back color is still unchanged either.
- Edited by Ian3 Wednesday, June 20, 2018 12:10 PM
Wednesday, June 20, 2018 4:07 AM