Freeze the 1st column in a grid, Posh Studio 2018

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 5 years and 4 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
User avatar
carrdav
Posts: 6
Last visit: Thu Nov 29, 2018 10:07 am

Freeze the 1st column in a grid, Posh Studio 2018

Post by carrdav »

Is there an easy way to do that? Similar to Excel where you can freeze the first column. I have an app that checks on servers and brings back multiple fields. The first field is SERVERNAME and as I scroll horizontally I'd like the 1st column (SERVERNAME) to stay in view as a reference as I scroll past things like RAM, CPU, etc..

Not sure where to begin.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Freeze the 1st column in a grid, Posh Studio 2018

Post by jvierra »

Yes. Just set the column to "Frozen" when you create it.
User avatar
carrdav
Posts: 6
Last visit: Thu Nov 29, 2018 10:07 am

Re: Freeze the 1st column in a grid, Posh Studio 2018

Post by carrdav »

Where would I do that exactly? I'm doing one of these:

Code: Select all

$DataCollectorSet | Add-Member -Type NoteProperty -Name "SpecMVVFCBM" -Value $SpecMVVFCBM 
$DataCollectorSet | Add-Member -Type NoteProperty -Name "OSServername" -Value $CS.Name
#bla bla bla

$DataList += $DataCollectorSet

# Then I stuff the array into the datagrid
$table = ConvertTo-DataTable -InputObject $Datalist #–FilterWMIProperties
Load-DataGridView -DataGridView $datagridviewResults -Item $table
I have an event that sorts the columns Ascending or Descending when clicked. I'm not sure where I would put the column freeze thing. Do you have a code example?

Code: Select all

$datagridviewResults_ColumnHeaderMouseClick = [System.Windows.Forms.DataGridViewCellMouseEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellMouseEventArgs]
	if ($datagridviewResults.DataSource -is [System.Data.DataTable])
	{
		$column = $datagridviewResults.Columns[$_.ColumnIndex]
		$direction = [System.ComponentModel.ListSortDirection]::Ascending
		
		if ($column.HeaderCell.SortGlyphDirection -eq 'Descending')
		{
			$direction = [System.ComponentModel.ListSortDirection]::Descending
		}
		
		$datagridviewResults.Sort($datagridviewResults.Columns[$_.ColumnIndex], $direction)
	}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Freeze the 1st column in a grid, Posh Studio 2018

Post by jvierra »

Add the column to the grid and select the frozen checkbox. It is on the bottom of the "Add" dialog.
User avatar
carrdav
Posts: 6
Last visit: Thu Nov 29, 2018 10:07 am

Re: Freeze the 1st column in a grid, Posh Studio 2018

Post by carrdav »

Ahh, I see, awesome thanks! I gave it a go and it gave me a duplicate Servername column but it's frozen!
Unfortunately since my code builds the columns on the fly I have to do it programmatically in one of the functions.

Something like:

Code: Select all

$dataGridView1.Columns[0].Frozen = $true;

$dataGridView1.Columns["Servername"].Frozen = $true;
I just have to translate from the C# examples I've seen and stick it in the right spot.
User avatar
carrdav
Posts: 6
Last visit: Thu Nov 29, 2018 10:07 am

Re: Freeze the 1st column in a grid, Posh Studio 2018

Post by carrdav »

Got it. I had to reference it as $dataGridViewResults.

Code: Select all

Load-DataGridView -DataGridView $datagridviewclusters -Item $tableClusters

# Putting this AFTER My Load-DataGridView function. 
$dataGridViewResults.Columns["Servername"].Frozen = $true;
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Freeze the 1st column in a grid, Posh Studio 2018

Post by jvierra »

No. Set the lead column "DataPropertyName" name to the name of the column you want to display and that will be the only one. All other columns will be auto generated.

Attached is a full demo. No changes to the grid other than adding a frozen column in the designer.
Attachments
Demo-DGVFrozenColumn.psf
(19.58 KiB) Downloaded 114 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Freeze the 1st column in a grid, Posh Studio 2018

Post by jvierra »

carrdav wrote: Wed Nov 28, 2018 7:02 pm Got it. I had to reference it as $dataGridViewResults.
Yes. That can work.
User avatar
carrdav
Posts: 6
Last visit: Thu Nov 29, 2018 10:07 am

Re: Freeze the 1st column in a grid, Posh Studio 2018

Post by carrdav »

It worked but I will check your example out too, thanks!
This topic is 5 years and 4 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