Page 1 of 1

ComboBox input to DataGridView

Posted: Tue Sep 14, 2021 12:12 pm
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?

Re: ComboBox input to DataGridView

Posted: Tue Sep 14, 2021 2:36 pm
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.

Re: ComboBox input to DataGridView

Posted: Wed Sep 15, 2021 7:48 am
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.

Re: ComboBox input to DataGridView

Posted: Wed Sep 15, 2021 8:04 am
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.

Re: ComboBox input to DataGridView

Posted: Wed Sep 15, 2021 8:13 am
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.

Re: ComboBox input to DataGridView

Posted: Thu Sep 16, 2021 8:04 am
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.