DataGridView - cell as button 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.
Post Reply
User avatar
pls-sapien
Posts: 18
Joined: Tue Apr 05, 2016 9:03 pm

DataGridView - cell as button column

Post by pls-sapien » Tue Sep 12, 2017 10:48 pm

hi all,
i could use some help in adjusting a specific column in a DataGridView to buttons.

this script connects to RDS Farm and returns some details from the session hosts available.
i want column 7 to be buttons that eventually will trigger reboot for the corresponding session host.
i use a grid template for tests and this is what i have so far:

Code: Select all

$CollectionName = "Terminal Test"
$BrokerHAInfo = Get-RDConnectionBrokerHighAvailability -ConnectionBroker broker01.domain.local
Get-RDSessionCollection -ConnectionBroker $BrokerHAInfo.ActiveManagementServer
$data = Get-RDSessionHost -CollectionName $CollectionName -ConnectionBroker $BrokerHAInfo.ActiveManagementServer
$table = New-Object System.Data.DataTable
$c1 = $table.Columns.Add("SessionHost", "string")
$c2 = $table.Columns.Add("DrainEnabled", "boolean")
$c3 = $table.Columns.Add("Users", "int32")
$c4 = $table.Columns.Add("LastClearProfiles", "datetime")
$c5 = $table.Columns.Add("LastClearPrinters", "datetime")
$c6 = $table.Columns.Add("LastClearTasks", "datetime")
$button7 = New-Object System.Windows.Forms.DataGridViewButtonColumn
$button7.HeaderText = "LastReboot"
$button7.Text = "LastReboot-Text"
$button7.Width = 120
$c7 = $table.Columns.Add($button7)
$c7.ColumnName = "LastReboot"

$DataCollectionTemp = Get-RDUserSession -ConnectionBroker $BrokerHAInfo.ActiveManagementServer -CollectionName $CollectionName
foreach ($item in $data)
{
	$r = $table.NewRow()
	
	$r.SessionHost = $item.SessionHost
	if ($item.NewConnectionAllowed -eq "yes")
	{
		$r.DrainEnabled = $true
	}
	Else
	{
		$r.DrainEnabled = $false
	}
	$r.users = ($DataCollectionTemp | where { $_.HostServer -eq $item.SessionHost }).Count
	
	$r.LastReboot = $button7.Text
	
	$table.Rows.Add($r)
	
}
but i get the last column as text...

thank you for any tips and information
Sean

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

Re: DataGridView - cell as button column

Post by jvierra » Tue Sep 12, 2017 11:27 pm

Is this a PSS project? The column in the DataTable has to be a Boolean and it must be in the table. Loading the grid will automatically create the correct column types. A Boolean should generate a checkbox.

You can also predefine the columns so they are named the same as the table columns. In the designer insert the column as a button column.

User avatar
pls-sapien
Posts: 18
Joined: Tue Apr 05, 2016 9:03 pm

Re: DataGridView - cell as button column

Post by pls-sapien » Tue Sep 12, 2017 11:57 pm

Hi jvierra,
thank you for your reply.
yes it is a powershell studio project (grid template). when i tried to configure the DataGrid in advance using the designer - all the column configuration was overwritten by the udpate-datagrid function or my own table updates...

thank you again,
Sean

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

Re: DataGridView - cell as button column

Post by jvierra » Wed Sep 13, 2017 12:13 am

The DataTable must be assigned using the DGV "DataSource" property and the column names - not just the headers - must match the table column names exactly. Table updates must be applied at the row level. The structure of the table cannot be changed after it is created and assigned.

User avatar
pls-sapien
Posts: 18
Joined: Tue Apr 05, 2016 9:03 pm

Re: DataGridView - cell as button column

Post by pls-sapien » Sat Sep 16, 2017 10:17 pm

Hi jvierra,

thank you for pointing me to the right direction, my problem was putting my data in a DataTable and not directly in the DataGridView.

i was wondering if you could help me with another problem im having:
i added a button in the DVG and i want to pop up a confirmation dialog that user can approve or cancel the operation.
so i thought i could use this basic flow of events:
1. CellMouseClick
2. pop up a message box with OK:Cancel
3. if ok - endedit (not sure if its end or commit), if cancel - canceledit

am i in the right direction?

thank you again for all the help!
Sean

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

Re: DataGridView - cell as button column

Post by jvierra » Sun Sep 17, 2017 4:29 am

Sounds good to me.

One note of direction. When we build grids the usual command method is to use a “context” menu. That is why most controls have a property where you can assign the context menu and the control display it for you. This allows for many commands to be added to manage an item in a list, grid, view or other control. I usually alos assign one context item to the “doubleclick” of the control as the default action.

The reason you had trouble with the button is because you are trying to use it in a way that it was not designed to be used making finding how to get what you wanted much harder.

Post Reply