How to set the datagridview column's width

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
1guy44
Posts: 4
Last visit: Sat Aug 03, 2019 3:32 am

How to set the datagridview column's width

Post by 1guy44 »

Hi everybody and sorry for my English,

I am creating powershell graphical interface, and I would like to display in a datagridview the result of my get-acl command.
Currently I can not define the width of columns in my datagridview so columns don't use the full size of the object.

Here is my code now:

Code: Select all

$gacl = Get-Acl -path $textbox_saisie_principal.Text

$gacl_access = $gacl.access | Select @{n='Objet';e={$_.IdentityReference}},@{n='Droits';e={$_.FileSystemRights}},@{n='Type';e={$_.AccessControlType}},@{n='Hérité';e={$_.IsInherited}},@{n='objet herité';e={$_.InheritanceFlags}}

$list_gacl = New-Object System.collections.ArrayList
$list_gacl.AddRange($gacl_access)

$grid = New-Object System.Windows.Forms.DataGridView
$grid.Name = "grid"
$grid.DataSource = $list_gacl
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 10
$System_Drawing_Point.Y = 118
$grid.Location = $System_Drawing_Point
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 400
$System_Drawing_Size.Width = 560
$grid.Size = $System_Drawing_Size
$grid.ColumnHeadersVisible = $true
$grid.ReadOnly = $true
$grid.RowHeadersVisible = $false

$main_form.Controls.Add($grid)
By doing this I can not define the width of my datagridview columns and the default width (100) is defined:

Image

Thank you in advance for your help
Last edited by 1guy44 on Sun May 19, 2019 4:36 am, edited 4 times in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to set the datagridview column's width

Post by jvierra »

Here is the correct way to set forms values in PowerShell and the correct way to set up and apply the "DataSource".

Code: Select all

$gacl = Get-Acl -path $textbox_saisie_principal.Text | 
    Select-Object 
        @{ n = 'Objet'; e = { $_.IdentityReference } }, 
        @{ n = 'Droits'; e = { $_.FileSystemRights } }, 
        @{ n = 'Type'; e = { $_.AccessControlType } }, 
        @{ n = 'Hérité'; e = { $_.IsInherited } }, 
        @{ n = 'objet herité'; e = { $_.InheritanceFlags } }

$grid = New-Object System.Windows.Forms.DataGridView
$main_form.Controls.Add($grid)
$grid.Name = "grid"
$grid.DataSource = [System.Collections.ArrayList]$gacl
$grid.Location = '10,118'
$grid.Size = '560,400'
$grid.ReadOnly = $true
$grid.AutoSizeColumnsMode = 'AllCells'
The following is how to set the columns to size correctly.

$grid.AutoSizeColumnsMode = 'AllCells'
1guy44
Posts: 4
Last visit: Sat Aug 03, 2019 3:32 am

Re: How to set the datagridview column's width

Post by 1guy44 »

A huge thank you to you jvierra you are the best !!!
I modfied your code to get this:

Code: Select all

$gacl = $(Get-Acl -path $textbox_saisie_principal.Text).Access | Select-Object @{ n='Objet';e={ $_.IdentityReference } },@{ n='Droits';e={ $_.FileSystemRights } },@{ n='Type';e={ $_.AccessControlType } }
$grid = New-Object System.Windows.Forms.DataGridView
$grid.ReadOnly = $true
$grid.RowHeadersVisible = $False
$grid.DataSource = [System.Collections.ArrayList]$gacl
$main_form.Controls.Add($grid)
$grid.Name = "grid"
$grid.Location = '10,118'
$grid.Size = '560,400'
$grid.AutoSizeColumnsMode = 'Fill'
It works exactly as I wanted:

Image
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to set the datagridview column's width

Post by jvierra »

That is because you have only one object. The following will fix this as longe as yout ACL is not null.

$grid.DataSource = [System.Collections.ArrayList]@($gacl)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to set the datagridview column's width

Post by jvierra »

Actually your select statement is also bad.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to set the datagridview column's width

Post by jvierra »

Sorry - I forgot one line:

Code: Select all

$properties = @(
    @{ n = 'Objet'; e = { $_.IdentityReference } },
    @{ n = 'Droits'; e = { $_.FileSystemRights } },
    @{ n = 'Type'; e = { $_.AccessControlType } },
    @{ n = 'Hérité'; e = { $_.IsInherited } },
    @{ n = 'objet herité'; e = { $_.InheritanceFlags } }
)
$gacl = Get-Acl test.bat |
    Select-Object -ExpandProperty Access |
    Select-Object $properties

$grid = New-Object System.Windows.Forms.DataGridView
$main_form.Controls.Add($grid)
$grid.Name = "grid"
$grid.DataSource = [System.Collections.ArrayList]$gacl
$grid_retour_hote_ACL.Location = '10,118'
$grid.Size = '560,400'
$grid.ColumnHeadersVisible = $true
$grid.ReadOnly = $true
$grid.AutoSizeColumnsMode = 'AllCells'

1guy44
Posts: 4
Last visit: Sat Aug 03, 2019 3:32 am

Re: How to set the datagridview column's width

Post by 1guy44 »

Thanks again for your help and your understanding, I just modified my code as below:

Code: Select all

$properties = @(
    @{ n = 'Objet'; e = { $_.IdentityReference } },
    @{ n = 'Droits'; e = { $_.FileSystemRights } },
    @{ n = 'Type'; e = { $_.AccessControlType } }
)
$gacl = Get-Acl $textbox_saisie_principal.Text |
    Select-Object -ExpandProperty Access |
    Select-Object $properties
$grid_retour_hote_ACL.RowHeadersVisible = $False
$grid_retour_hote_ACL.BackgroundColor = 'black'
$grid_retour_hote_ACL.ForeColor= [System.Drawing.Color]::FromArgb(255,0,0,255)

$page_principal.Controls.Add($grid_retour_hote_ACL)
$grid_retour_hote_ACL.Name = "grid_retour_hote_ACL"
$grid_retour_hote_ACL.DataSource = [System.Collections.ArrayList]$gacl
$grid_retour_hote_ACL.Location = '10,118'
$grid_retour_hote_ACL.Size = '560,400'
$grid_retour_hote_ACL.ColumnHeadersVisible = $true
$grid_retour_hote_ACL.ReadOnly = $true
$grid_retour_hote_ACL.AutoSizeColumnsMode = 'Fill'
and it works perfectly !! :

Image
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How to set the datagridview column's width

Post by jvierra »

The easy nway to do colors:

$grid_retour_hote_ACL.ForeColor = 0xff0000ff
1guy44
Posts: 4
Last visit: Sat Aug 03, 2019 3:32 am

Re: How to set the datagridview column's width

Post by 1guy44 »

Thank you I just changed all my code in this direction for the colors. Otherwise I do not know if it is possible but I would like to add 2 textbox and a button to enter the username and password of a user account that has the rights to run the same get-acl command and therefore list rights.

like this:
Image
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked