$datagridview1.AutoSizeColumnsMode = 'Fill' - howto size after cell content?

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.
Post Reply
User avatar
lontru
Posts: 77
Joined: Fri Aug 18, 2017 4:36 am

$datagridview1.AutoSizeColumnsMode = 'Fill' - howto size after cell content?

Post by lontru »

Code: Select all

$form1_Load={
	#TODO: Initialize Form Controls here
	$VMs = Get-VM | Select-Object Name, CPUUsage, UpTime
	$DataTable = ConvertTo-DataTable -InputObject $VMs
	$datagridview1.RowHeadersVisible = $false
	$datagridview1.AutoSizeColumnsMode = 'Fill'
	Update-DataGridView -DataGridView $datagridview1 -Item $DataTable
}
I get this when i run my code:
Image

How do i get it to size to the content in the cell? like this
Image

jvierra
Posts: 14702
Joined: Tue May 22, 2007 9:57 am
Answers: 8
Has voted: 2 times
Been upvoted: 6 times

Re: $datagridview1.AutoSizeColumnsMode = 'Fill' - howto size after cell content?

Post by jvierra »

You can set the width in the designer or you can do it in code.

User avatar
lontru
Posts: 77
Joined: Fri Aug 18, 2017 4:36 am

Re: $datagridview1.AutoSizeColumnsMode = 'Fill' - howto size after cell content?

Post by lontru »

is there a way so it dynamic cause the name lenght can changes`?

jvierra
Posts: 14702
Joined: Tue May 22, 2007 9:57 am
Answers: 8
Has voted: 2 times
Been upvoted: 6 times

Re: $datagridview1.AutoSizeColumnsMode = 'Fill' - howto size after cell content?

Post by jvierra »

The length of the columns changes dynamically to the width of the column of text when you set the mode to do so. YOU cannot set bot "Fill" and autosize at the same time.

Post Reply