DataGridView button column

Ask your Windows 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.
Locked
PaschalIT
Posts: 38
Joined: Tue Apr 02, 2019 4:37 pm

DataGridView button column

Post by PaschalIT » Thu Apr 18, 2019 12:01 pm

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: 14010
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView button column

Post by jvierra » Thu Apr 18, 2019 12:55 pm

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: 14010
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView button column

Post by jvierra » Thu Apr 18, 2019 1:07 pm

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
Joined: Tue Apr 02, 2019 4:37 pm

Re: DataGridView button column

Post by PaschalIT » Thu Apr 18, 2019 2:22 pm

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: 14010
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView button column

Post by jvierra » Thu Apr 18, 2019 2:36 pm

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
Joined: Tue Apr 02, 2019 4:37 pm

Re: DataGridView button column

Post by PaschalIT » Thu Apr 18, 2019 2:51 pm

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: 14010
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: DataGridView button column

Post by jvierra » Thu Apr 18, 2019 2:58 pm

So...just use Get-Process to see if the program is running.

Locked