I need to freeze a column

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 6 years and 2 days 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
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

I need to freeze a column

Post by jsira2003@yahoo.com »

$form1BaselineandScanSummaries_Load={

#TODO: Initialize Form Controls here
Get-DbInfo
Import-Module SQLPS -DisableNameChecking
$query = "
USE [mydatabase]
Select convert(varchar,startdatetime,120) AS DateTime,SharePath,Operation AS Process,Status,QtyOfFilesBaselined AS Baselined,QtyOfFilesChecked AS QtyCkd,Missing,ChangedHash AS BadHash,ReportPath AS ReportFile FROM BaselineScanLog Order By startDateTime desc
"
[System.Collections.ArrayList]$BaselineScanLogs = Invoke-Sqlcmd -Query $query -ServerInstance $global:instanceName -Database $global:dbName
$BaselineScanLogs = $BaselineScanLogs | Select-Object * -ExcludeProperty ROWSTATE, HASERRORS, ROWERROR, TABLE, ITEMARRAY
#$LogsScrubbedDT = ConvertTo-DataTable -Item $BaselineScanLogs -FilterWMIProperties
$dtable = $BaselineScanLogs | Out-DataTable
#$datagridview1.datasource = $dtable
Update-DataGridView -DataGridView $datagridview1 -Item $dtable -AutoSizeColumns DisplayedCells


}

#region Control Helper Functions
function Update-DataGridView
{
<#
.SYNOPSIS
This functions helps you load items into a DataGridView.

.DESCRIPTION
Use this function to dynamically load items into the DataGridView control.

.PARAMETER DataGridView
The DataGridView control you want to add items to.

.PARAMETER Item
The object or objects you wish to load into the DataGridView's items collection.

.PARAMETER DataMember
Sets the name of the list or table in the data source for which the DataGridView is displaying data.

.PARAMETER AutoSizeColumns
Resizes DataGridView control's columns after loading the items.
#>
Param (
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
[System.Windows.Forms.DataGridView]$DataGridView,
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
$Item,
[Parameter(Mandatory=$false)]
[string]$DataMember,
[System.Windows.Forms.DataGridViewAutoSizeColumnMode]$AutoSizeColumns = 'None'
)
$DataGridView.SuspendLayout()
$DataGridView.DataMember = $DataMember

if ($Item -is [System.Data.DataSet] -and $Item.Tables.Count -gt 0)
{
$DataGridView.DataSource = $Item.Tables[0]
}
elseif ($Item -is [System.ComponentModel.IListSource]`
-or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] )
{
$DataGridView.DataSource = $Item
}
else
{
$array = New-Object System.Collections.ArrayList

if ($Item -is [System.Collections.IList])
{
$array.AddRange($Item)
}
else
{
$array.Add($Item)
}
$DataGridView.DataSource = $array
}

if ($AutoSizeColumns -ne 'None')
{
$DataGridView.AutoResizeColumns($AutoSizeColumns)
}

$DataGridView.ResumeLayout()
}

function ConvertTo-DataTable
{
<#
.SYNOPSIS
Converts objects into a DataTable.

.DESCRIPTION
Converts objects into a DataTable, which are used for DataBinding.

.PARAMETER InputObject
The input to convert into a DataTable.

.PARAMETER Table
The DataTable you wish to load the input into.

.PARAMETER RetainColumns
This switch tells the function to keep the DataTable's existing columns.

.PARAMETER FilterWMIProperties
This switch removes WMI properties that start with an underline.

.EXAMPLE
$DataTable = ConvertTo-DataTable -InputObject (Get-Process)
#>
[OutputType([System.Data.DataTable])]
param(
[ValidateNotNull()]
$InputObject,
[ValidateNotNull()]
[System.Data.DataTable]$Table,
[switch]$RetainColumns,
[switch]$FilterWMIProperties)

if($null -eq $Table)
{
$Table = New-Object System.Data.DataTable
}

if ($InputObject -is [System.Data.DataTable])
{
$Table = $InputObject
}
elseif ($InputObject -is [System.Data.DataSet] -and $InputObject.Tables.Count -gt 0)
{
$Table = $InputObject.Tables[0]
}
else
{
if (-not $RetainColumns -or $Table.Columns.Count -eq 0)
{
#Clear out the Table Contents
$Table.Clear()

if ($null -eq $InputObject) { return } #Empty Data

$object = $null
#find the first non null value
foreach ($item in $InputObject)
{
if ($null -ne $item)
{
$object = $item
break
}
}

if ($null -eq $object) { return } #All null then empty

#Get all the properties in order to create the columns
foreach ($prop in $object.PSObject.Get_Properties())
{
if (-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__')) #filter out WMI properties
{
#Get the type from the Definition string
$type = $null

if ($null -ne $prop.Value)
{
try { $type = $prop.Value.GetType() }
catch { Out-Null }
}

if ($null -ne $type) # -and [System.Type]::GetTypeCode($type) -ne 'Object')
{
[void]$table.Columns.Add($prop.Name, $type)
}
else #Type info not found
{
[void]$table.Columns.Add($prop.Name)
}
}
}

if ($object -is [System.Data.DataRow])
{
foreach ($item in $InputObject)
{
$Table.Rows.Add($item)
}
return @( ,$Table)
}
}
else
{
$Table.Rows.Clear()
}

foreach ($item in $InputObject)
{
$row = $table.NewRow()

if ($item)
{
foreach ($prop in $item.PSObject.Get_Properties())
{
if ($table.Columns.Contains($prop.Name))
{
$row.Item($prop.Name) = $prop.Value
}
}
}
[void]$table.Rows.Add($row)
}
}

return @(,$Table)
}
#endregion

$datagridview1_CellClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
#TODO: Place custom script here
if ($_.ColumnIndex -eq 8)
{
$Global:value = $datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value
$textbox1Msg.Text = "Loading Report Please Wait..."
$textbox1Msg.Refresh()
sleep 2
Show-ReportViewer_psf
$textbox1Msg.Text = "Sort Columns by Clicking on the Column Headers. Click on the Report to View the Report in the Report Viewer"
$textbox1Msg.Refresh()
sleep 2
}
}

$datagridview1_ColumnHeaderMouseClick=[System.Windows.Forms.DataGridViewCellMouseEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellMouseEventArgs]
#TODO: Place custom script here
if ($datagridview1.DataSource -is [System.Data.DataTable])
{
$column = $datagridview1.Columns[$_.ColumnIndex]
$direction = [System.ComponentModel.ListSortDirection]::Ascending

if ($column.HeaderCell.SortGlyphDirection -eq 'Descending')
{
$direction = [System.ComponentModel.ListSortDirection]::Descending
}

$datagridview1.Sort($datagridview1.Columns[$_.ColumnIndex], $direction)
}
}


How can I freeze my datagrid for example on column 3?

Thank you!
John
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: I need to freeze a column

Post by jsira2003@yahoo.com »

I figured it out!

thanks,
john
User avatar
StillLearning
Posts: 39
Last visit: Tue Apr 10, 2018 9:39 pm

Re: I need to freeze a column

Post by StillLearning »

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

Re: I need to freeze a column

Post by jvierra »

When posting more than a few lines of code please attach the code as a file. Reading and copying large blocks of code in the editor is very hard and can cause the copy to be broken.

A DataGridViewColumn has a property that sets a column as "fixed" in position. In the designer it is a checkbox in the column editor labeled "Frozen". As property it is accessed this way:

$datagridview1.Columns[0].Frozen = $true
This topic is 6 years and 2 days 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