Support Forums

Making data grid item a hyperlink

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
User avatar
rocky.pabillore
Posts: 5
Joined: Thu Aug 25, 2016 7:05 am

Making data grid item a hyperlink

Postby rocky.pabillore » Fri Apr 21, 2017 7:58 am

Hi everyone, I have a simple but really difficult task on creating our "Printer Status hub".

I want the portname to be a link so i can open it via default browser.
I know I have to specify it but I can't figure this out.


#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

$formMain_Load={
#TODO: Initialize Form Controls here

}

$buttonExit_Click={
#TODO: Place custom script here
$formMain.Close()
}

$buttonLoad_Click= {
#TODO: Place custom script here
# ---------------------------------
# Sample Code to Load Grid
# ---------------------------------
$printerservers = ("server01","server02")
ForEach ($printerserver in $PrinterServers)
{
$name = get-printer -ComputerName $printerserver | Where-Object { $_.PrinterStatus -eq "Normal" } | Select-Object Name, portname, printerstatus
}
$printers = $name
Update-DataGridView -DataGridView $datagridviewResults -Item $printers-AutoSizeColumns DisplayedCells


# ---------------------------------
# Sample Code to Load Sortable Data
# ---------------------------------
# $processes = Get-WmiObject Win32_Process -Namespace "Root\CIMV2"
# $table = ConvertTo-DataTable -InputObject $processes -FilterWMIProperties
# Update-DataGridView -DataGridView $datagridviewResults -Item $table -AutoSizeColumns DisplayedCells
}

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

$datagridviewResults_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
#TODO: Place custom script here

}

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

Re: Making data grid item a hyperlink

Postby jvierra » Fri Apr 21, 2017 9:40 am

Not sure what you are trying to ask.

To get a hyperlink column you will have to create a column that is designed as a hyperlink an bind the column to the field you want as a hyperlink.
User avatar
rocky.pabillore
Posts: 5
Joined: Thu Aug 25, 2016 7:05 am

Re: Making data grid item a hyperlink

Postby rocky.pabillore » Fri Apr 21, 2017 12:07 pm

Where do you set that?
User avatar
jvierra
Posts: 10634
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Making data grid item a hyperlink

Postby jvierra » Fri Apr 21, 2017 12:13 pm

In the designer add the column and select hyperlink as the column control type.

Return to “Windows PowerShell”

Who is online

Users browsing this forum: No registered users and 3 guests