Page 1 of 1

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

Posted: Wed Nov 28, 2018 3:26 pm
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.

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

Posted: Wed Nov 28, 2018 4:02 pm
by jvierra
Yes. Just set the column to "Frozen" when you create it.

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

Posted: Wed Nov 28, 2018 5:55 pm
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)
	}
}

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

Posted: Wed Nov 28, 2018 6:25 pm
by jvierra
Add the column to the grid and select the frozen checkbox. It is on the bottom of the "Add" dialog.

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

Posted: Wed Nov 28, 2018 6:34 pm
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.

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

Posted: Wed Nov 28, 2018 7:02 pm
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;

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

Posted: Wed Nov 28, 2018 7:03 pm
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.

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

Posted: Wed Nov 28, 2018 7:09 pm
by jvierra
carrdav wrote: ↑Wed Nov 28, 2018 7:02 pm Got it. I had to reference it as $dataGridViewResults.
Yes. That can work.

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

Posted: Thu Nov 29, 2018 10:07 am
by carrdav
It worked but I will check your example out too, thanks!