DataGridView button column

Ask your PowerShell-related questions, including questions on cmdlet development!
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 4 years and 11 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
PaschalIT
Posts: 38
Last visit: Fri Jan 10, 2020 7:38 am

DataGridView button column

Post by PaschalIT »

Hi everyone!

This time I'm trying to add a button column to a DataGridView control, but I only want the button in each row to be enabled if certain parameters are met. What I'm having trouble with is designating the button I'm trying to disable/enable. I don't really have a code example because I'm pretty sure I'm doing it wrong anyways...

What I do know I'm doing right (with my variables substituted out, of course):
  1. $datagridview1.Rows.Add("<Computer Name>","<Program Version>","<Date Updated>")
This populates all my computer names properly, along with the version and update date of each. The 4th column is the button column I'm referring to. The designer automatically adds a button for each row, which is great. I want to disable the ones that don't meet a certain criteria.

Any suggestions from anyone with this weak description? Any help is appreciated. If you have any other questions I'll do my best to answer.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView button column

Post by jvierra »

You will have to manage the buttons programmatically but, unfortunately, this cannot be done with PowerShell. You will have to write C# code to alter the buttons behavior as the control in the cell is not exposed to the PS environment.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView button column

Post by jvierra »

The important thing to note is that the "CellButton" has no events and is not a true button. It has no "enabled" or "visible" properties. It exisits as a pure slave of the grid control and just displays text. The grid handles the CellClick event. You can, possibly, use the cell click to ignore the click for specific buttons. You can also use the "CellPaint" to alter the color of the button.
PaschalIT
Posts: 38
Last visit: Fri Jan 10, 2020 7:38 am

Re: DataGridView button column

Post by PaschalIT »

Hmmm, that's unfortunate... That's kind of the same conclusion I was beginning to draw through the research I've done so far.

Would my best solution be possibly to build a boolean array that controls whether a button is "enabled" or not, and check in the DataGridView's cellclick event to make sure it's the cell in the last column that's clicked, and that it matches one of the "enabled" indexes in my array? That's the main idea coming to mind.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView button column

Post by jvierra »

Something like that. What is it that tells you the button is disabled? Just evaluate that condition in the click event. No need to create any arrays.
PaschalIT
Posts: 38
Last visit: Fri Jan 10, 2020 7:38 am

Re: DataGridView button column

Post by PaschalIT »

I want the button to kill a process when clicked on the PC listed in that row, but I only want the button to display (or at least only be clickable) if that PC has the program I'm killing already running.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView button column

Post by jvierra »

So...just use Get-Process to see if the program is running.
This topic is 4 years and 11 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