Request for help with DataGridView, (Header) Checkboxes, Check/Uncheck all and Loops

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.
EnhancedDeflate
Posts: 6
Joined: Fri Aug 03, 2018 1:26 am

Request for help with DataGridView, (Header) Checkboxes, Check/Uncheck all and Loops

Post by EnhancedDeflate » Fri Aug 03, 2018 2:20 am

First of all: Hi, I'm Toby from Germany and unfortunately I am not a programmer.

I still like PowerShell though and do tinker with a few scripts from time to time, and some I would like to improve with a graphical user interface. I'm using PowerShell Studio 2012 (v3.1.17) my company bought a few years back. I execute it on a W7 x64 virtual machine.

Anyway I think I can easily explain my problem:
I want to display a DataGridView.
First column will display a Checkbox, second column will display a hostname entry (will list ESXi servers dynamically here later) and the third column will display a graphic icon representing the status of whether or not a change was successful.

This is how it looks like at the moment:
2018-08-03_110213.jpg
2018-08-03_110213.jpg (40.74 KiB) Viewed 441 times
Now what I need or better what I would kindly ask is to be able to have a Check/Uncheck all functionality done in the header column as you probably know it. Click it once when no checkbox below is checked and it will automatically select all below checkboxes. Click it again and will deselect all checkboxes. If some are already selected, they will be all selected with a click on the header checkbox.

Then I will have to loop through the items that are in the DataGridView list and make sure to apply changes only to those that are actually selected, represented by that checkbox.

When this works well I would like to have an icon show - as described above - whether the change was successful or not so this needs to be refreshed based on the selections done and also based on the results of the underlying script.

What I can offer, to not seem to be completely helpless or just wanting to avail myself on your skills:
I'm able to make use of a DataGridView and to fill it with data. I know that there are different ColumnTypes I can select (e.g. ColumnType = DataGridViewCheckBoxColumn) and I think I'm able to do all the coding required to make my scripts work - but unfortunately fail with the GUI.

Is there anybody that could help me with my request or is it too specific? I know coding takes time and since I would like to re-use these controls in other parts of my script I only need the inital setup, if possible, so that I can learn from it and copy it to other parts.

Thanks to you guys in advance and best regards
ED

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

Re: Request for help with DataGridView, (Header) Checkboxes, Check/Uncheck all and Loops

Post by jvierra » Fri Aug 03, 2018 2:34 am

There is an event called when the header column is clicked. You use the event to alter the rows however you want.

Your question is pretty vague. You cannot use or move controls from a DGV to other parts of a script. What is it that you are trying to do with these controls.

You can access any control from anywhere in the script by using the controls variable.

You can learn about GUI script with PowerShell from these articles: https://info.sapien.com/index.php/guis/gui-scripting

EnhancedDeflate
Posts: 6
Joined: Fri Aug 03, 2018 1:26 am

Re: Request for help with DataGridView, (Header) Checkboxes, Check/Uncheck all and Loops

Post by EnhancedDeflate » Fri Aug 03, 2018 7:34 am

jvierra wrote:
Fri Aug 03, 2018 2:34 am
Your question is pretty vague. You cannot use or move controls from a DGV to other parts of a script. What is it that you are trying to do with these controls.
Dear jvierra,

first of all thank you for your quick reply.

I don't want to move controls from a DGV elsewhere, this is a misunderstanding. It was a note on re-using code for other tabpages with other DataGridView elements and that I would need just one example so that I would be able to adjust it to my needs.

So it's not clear what I want? If not, please read the following paragraph:

Upon opening the tabpage in the application window this DataGridView is being filled dynamically by listing ESXi hosts in a vCenter (using PowerCLI). Each line lists one host. Also each line is supposed to start with a checkbox in the first column, the second shows the host itself and the third one will display an image representing a check result (OK or NOK).

Next to the DGV I will add a text input field where parameters can be set. Once set, this is supposed to be sent to those hosts currently selected with the referring checkbox. If the host is not selected it should not be affected by the script.

Do you know what I mean?

Thanks
ED

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

Re: Request for help with DataGridView, (Header) Checkboxes, Check/Uncheck all and Loops

Post by jvierra » Fri Aug 03, 2018 7:42 am

No. You want to add a textbox to a tab that somehow sends something to someplace. Not very clear.

In Forms all things happen because of events. What event is going to cause things to happen? THat event wil lcontain the script that performs your actions. The most common event control is a Button.

Start ehre to learn how to design forms that do admin and management functions: https://info.sapien.com/index.php/guis

There ae many articles that will help you understand how forms and controls work to produce the desired results.

EnhancedDeflate
Posts: 6
Joined: Fri Aug 03, 2018 1:26 am

Re: Request for help with DataGridView, (Header) Checkboxes, Check/Uncheck all and Loops

Post by EnhancedDeflate » Tue Aug 07, 2018 11:26 am

@jvierra: How am I supposed to showcase my result when I'm not able to do the proper programming yet?

You seem to look at the picture of the window and how it currently looks like and seem to know what I am about to do? Please read the text thoroughly. I'm asking for help and you are somehow being cocky and I don't know why.

I love to look at the tutorials and I'll be happy to learn something new, but that is not what I want to achieve here! And I certainly don't want to add a textbox to a tab (never said that)!

As I wrote in the little introduction I am not a programmer but I did some programming anyway. I know what events are, I know what buttons are and I have put together a few forms already. So I still think I described well that I want to have a checkbox in the header column where there is none by default to be able to select all items in the DGV or uncheck those with one click.

Just imagine the following scenario as this little animated gif perfectly describes what I want to do:
Image

Source
This is not mine, but this is what I'm aiming at, just with PowerShell and hopefully PowerShell Studio 2012.

I'm sorry. I am not a native speaker and I do my best to describe the problems I'm struggling with. I do not want to annoy you or anyone, I'm just asking for help and want to find out if what I want is possible.

Thanks and best regards
ED

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

Re: Request for help with DataGridView, (Header) Checkboxes, Check/Uncheck all and Loops

Post by jvierra » Tue Aug 07, 2018 11:36 am

Your question was hard to understand because you added many things that had nothing to do with what you wanted to as. Specifically:
Next to the DGV I will add a text input field where parameters can be set. Once set, this is supposed to be sent to those hosts currently selected with the referring checkbox. If the host is not selected it should not be affected by the script.
You cannot directly place a checkbox in the header in PowerShell. There is no template for that. The best you can do is to float a checkbox over the column and have its event check or uncheck all. You can call "BringtoFront()" on the checkbox so that it displays on top of the column as needed. Make the column test short so the checkbox hides it.

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

Re: Request for help with DataGridView, (Header) Checkboxes, Check/Uncheck all and Loops

Post by jvierra » Tue Aug 07, 2018 12:33 pm

After rereading you post a couple of times it seems to all boils down to "How to check all checkboxes in a DGV".

That and nearly all row level operations are done by addressing the row and cell objects.

$datagridview1.Rows | ForEach-Object{ $_.Cells['cell name'] … (property to view or alter) }

To check/uncheck all checkboxes just address the cell and se the value to $true or $false.

$datagridview1.Rows | %{$_.Cells['column1'].Value = [$true|$false]

To set the checks to the state of a checkbox just do this:

Code: Select all

$checkbox1_CheckedChanged={
	$datagridview1.Rows | %{$_.Cells['column1'].Value = $checkbox1.Checked}
}

EnhancedDeflate
Posts: 6
Joined: Fri Aug 03, 2018 1:26 am

Re: Request for help with DataGridView, (Header) Checkboxes, Check/Uncheck all and Loops

Post by EnhancedDeflate » Tue Aug 07, 2018 1:51 pm

jvierra wrote:
Tue Aug 07, 2018 12:33 pm
After rereading you post a couple of times it seems to all boils down to "How to check all checkboxes in a DGV".
First of all thank you for re-reading my post.
Yes, you could say it boils down to this Check/Uncheck functionality essentially.

I made a drawing. Please don't laugh, I did it on basis of the current screenshot and extended it with MSPAINT.
DGV-GUI-Draft.jpg
DGV-GUI-Draft.jpg (66.83 KiB) Viewed 367 times
This is what it finally should look like, just more 'neat'.
So what I originally meant was to have two textboxes that I can type parameters in. When clicking on the "Update Values" button it should do exactly this - update the values that currently exist on the Host machines and overwrite those with the information given in the textboxes. But this is another topic.

Sorry for being so complicated.
I will implement the check routine and will try to come up with a working solution following your example.

Thank you!
ED

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

Re: Request for help with DataGridView, (Header) Checkboxes, Check/Uncheck all and Loops

Post by jvierra » Tue Aug 07, 2018 2:03 pm

Still can't figure out what the textboxes m have to do with checking and unchecking all checkboxes. Somewhere you are either missing an important piece of code or information.

I also don't understand what you expect two textboxes to do. How do they relate to the code you are running?

EnhancedDeflate
Posts: 6
Joined: Fri Aug 03, 2018 1:26 am

Re: Request for help with DataGridView, (Header) Checkboxes, Check/Uncheck all and Loops

Post by EnhancedDeflate » Tue Aug 07, 2018 11:30 pm

jvierra wrote:
Tue Aug 07, 2018 2:03 pm
Still can't figure out what the textboxes m have to do with checking and unchecking all checkboxes. Somewhere you are either missing an important piece of code or information.

I also don't understand what you expect two textboxes to do. How do they relate to the code you are running?
Good morning jvierra.

This GUI I'm working on is supposed to ease the VMware administrator's mind. By using that tool one should be able to overwrite configuration settings on the ESXi hosts (so the hosts running virtual machines). I made the experience that many people asking for help leave out a lot of information which makes it hard to understand what they actually want. In my case I wanted to avoid all that by being comprehensively explaining the issue(s) I'm having but obviously failed :? sorry for that.

So to keep things simple:

Let's say you work with 30 ESXi hosts (in case you're not familiar with VMware let's just say these are servers). Each server is configured independently during or after the initial installation (unless you work with so called host profiles). Now for example when you change your corporate DNS or NTP settings or want to configure the hosts to use a different logfile storage location you need to do that on each host individually.
This tool is supposed to help simplifying that process. So rather than connecting to each host and entering that information manually this GUI should help executing that change on all hosts that are selected (thus the checkboxes) within the DataGridView window automatically.

Step-by-step:
When opening up that tapbage it will list all hosts that are available (given that one is connected to a vCenter instance) in that DGV. Then you may enter the updated IP address information, DNS information or whatever is required on this tabpage and press the Update Button so it will loop through all those (selected) entries and set the new values from the text input boxes.

Just as a background information this code I want to use already exists as I have created a few of them last year - but they currently run in the PowerShell window as .ps1 files.

Best regards
ED

Locked