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.
Locked
User avatar
carrdav
Posts: 6
Joined: Fri May 24, 2013 7:12 am

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

Post by carrdav » Wed Nov 28, 2018 4:26 pm

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.

User avatar
jvierra
Posts: 13174
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Nov 28, 2018 5:02 pm

Yes. Just set the column to "Frozen" when you create it.

User avatar
carrdav
Posts: 6
Joined: Fri May 24, 2013 7:12 am

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

Post by carrdav » Wed Nov 28, 2018 6:55 pm

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)
	}
}

User avatar
jvierra
Posts: 13174
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Nov 28, 2018 7:25 pm

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
Joined: Fri May 24, 2013 7:12 am

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

Post by carrdav » Wed Nov 28, 2018 7:34 pm

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
Joined: Fri May 24, 2013 7:12 am

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

Post by carrdav » Wed Nov 28, 2018 8:02 pm

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;

User avatar
jvierra
Posts: 13174
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Nov 28, 2018 8:03 pm

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 8 times

User avatar
jvierra
Posts: 13174
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Nov 28, 2018 8:09 pm

carrdav wrote:
Wed Nov 28, 2018 8:02 pm
Got it. I had to reference it as $dataGridViewResults.
Yes. That can work.

User avatar
carrdav
Posts: 6
Joined: Fri May 24, 2013 7:12 am

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

Post by carrdav » Thu Nov 29, 2018 11:07 am

It worked but I will check your example out too, thanks!

Locked