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.

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 6 months 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
pls-sapien
Posts: 31
Last visit: Tue Dec 19, 2023 12:55 am

DataGridView - cell as button column

Post by pls-sapien »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView - cell as button column

Post by jvierra »

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: 31
Last visit: Tue Dec 19, 2023 12:55 am

Re: DataGridView - cell as button column

Post by pls-sapien »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView - cell as button column

Post by jvierra »

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: 31
Last visit: Tue Dec 19, 2023 12:55 am

Re: DataGridView - cell as button column

Post by pls-sapien »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView - cell as button column

Post by jvierra »

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.
This topic is 6 years and 6 months 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