Page 1 of 1

DataGridView button column

Posted: Thu Apr 18, 2019 12:01 pm
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.

Re: DataGridView button column

Posted: Thu Apr 18, 2019 12:55 pm
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.

Re: DataGridView button column

Posted: Thu Apr 18, 2019 1:07 pm
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.

Re: DataGridView button column

Posted: Thu Apr 18, 2019 2:22 pm
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.

Re: DataGridView button column

Posted: Thu Apr 18, 2019 2:36 pm
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.

Re: DataGridView button column

Posted: Thu Apr 18, 2019 2:51 pm
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.

Re: DataGridView button column

Posted: Thu Apr 18, 2019 2:58 pm
by jvierra
So...just use Get-Process to see if the program is running.