ComboBox input to DataGridView

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.
Post Reply
User avatar
JProvencher
Posts: 7
Last visit: Mon Sep 20, 2021 9:04 am

ComboBox input to DataGridView

Post by JProvencher »

Currently using PowerShell Studio 2021, if it at all matters.

I've used it for coding but this is my first leap into GUI development. What I'm doing now is not more than a "How Do I" exercise.

I have a combobox which I'm able to populate with AD groups, where AdminCount = 1.

What I'm trying to do is populate a datagridview with the properties, and later down the road members, of the selected group.

$Global:GroupColl = Get-ADGroup -filter 'AdminCount -eq 1' | Select-Object -ExpandProperty Name
Update-Combobox $ComboBox1 -Items $Global:GroupColl

$Global:GrpInfo = (Get-ADGroup -Identity $Combobox1.Text -Server [domain] -Properties Name | Select-Object -Property Name)

Update-DataGridview $DataGridView1 -Item $Global:GrpInfo

The form displays and I can select a group from the combobox, but nothing populates in the datagridview. I'm sure I'm missing something. Maybe the Update-Datagridview needs to be linked to an event?

jvierra
Posts: 14930
Last visit: Mon Sep 20, 2021 8:13 am
Answers: 14
Has voted: 3 times
Been upvoted: 11 times

Re: ComboBox input to DataGridView

Post by jvierra »

First from what you posted I can only say that is not what a DGV is used for or how to use it.
A DGV takes objects or object collections and not strings. If you are using the object obtained from an AD query then the object has property names wich much match the column's "DataMember" name.

In your post there is not enough information to know what you are seeing or how the DGV has been defined.

Note that you cannot at objects to a DGV with differently defined structures.

User avatar
JProvencher
Posts: 7
Last visit: Mon Sep 20, 2021 9:04 am

Re: ComboBox input to DataGridView

Post by JProvencher »

It seems to me that getting the group name from the combobox to the Get-ADGroup cmdlet isn't working.

I looked at the post here: viewtopic.php?p=63473#p63473 and the code there worked, just changed datagridview6 to datagridview1:

Code: Select all

$Global:coll = (Get-ADUser -filter * -Properties PasswordNeverExpires, passwordlastset, lastlogon, enabled | select Name, Enabled, PasswordNeverExpires, PasswordLastset, @{ name = "LastLogon"; Expression = ({ get-date $_.lastlogon -UFormat "%D %R" }) })

Update-DataGridView $datagridview6 $coll
Here, the format of the DataGridView is not defined in Globals.ps1.

jvierra
Posts: 14930
Last visit: Mon Sep 20, 2021 8:13 am
Answers: 14
Has voted: 3 times
Been upvoted: 11 times

Re: ComboBox input to DataGridView

Post by jvierra »

Again, the code posted is dependent on the whole of the DGV configuration. WIthout all of the settings then there is no way to know what is happening.

Start simple. Create a simple form with a DGV and change no settings. Add one button and add the code you just posted.

When working just remove all properties except for "Name" and try again.

jvierra
Posts: 14930
Last visit: Mon Sep 20, 2021 8:13 am
Answers: 14
Has voted: 3 times
Been upvoted: 11 times

Re: ComboBox input to DataGridView

Post by jvierra »

Also note that global does not define any control. It is just a blank file that you can use to create global variables. Controls on a form do not need to be defined as "global" and the code you are writing should not declare simple variables as "global".

With Sapien PSS the control variables are defined local to the script by default and are totally visible to all elements of the form.

There are numerous articles in the "info center" on this site that will help you understand the use of "scope" in scripts and with forms.

Another thing that may be happening is that you may either have an empty collection or a collection with a single item that may need to be forced into an array. You can try changing your original code to be sure you return more than one object.

User avatar
JProvencher
Posts: 7
Last visit: Mon Sep 20, 2021 9:04 am

Re: ComboBox input to DataGridView

Post by JProvencher »

I've made some progress. Part of the trick was to bundle it to an OnClick event.
Right now, I have a combobox that gets populated with AD group names where AdminCount -eq 1.
Once I click the button, a textbox shows the distinguished name of the selected group. Below that, I have a DGV to show the distinguishedname of the members of selected group. Right now, it's showing a blank line for each member. I'll have a look around; I'm sure I'm overlooking something simple.

Post Reply