D
D
Denis Kolmykov2015-05-07 09:55:55
PowerShell
Denis Kolmykov, 2015-05-07 09:55:55

combo box. ValueMember and DisplayMember properties in powershell?

There is a code sketch, a simple display of containers from AD:

cls
[array]$test  = Get-ADOrganizationalUnit -filter * -SearchBase "dc=DOMAIN,dc=local"

Function funMainForm {
    $Global:main_form = New-Object System.Windows.Forms.Form
    $main_form.Text ='Окно'
    $main_form.Width = 600
    $main_form.Height = 480
    $main_form.AutoSize = $true

    $Global:cmbTest = New-Object System.Windows.Forms.ComboBox
    $cmbTest.Width = 620
    $cmbTest.DisplayMember = "name"
    $cmbTest.ValueMember = "DistinguishedName"
    $cmbTest.DataSource = $test
    $cmbTest.Location = New-Object System.Drawing.Point(10,50)
    $main_form.Controls.Add($cmbTest)

    $main_form.ShowDialog()
}

funMainForm

It is necessary that the name of the container is displayed in the combobox, but its path is displayed (as if the ValueMember and DisplayMember properties were not set at all). Can anyone suggest how to work with ValueMember and DisplayMember correctly and what is my mistake?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Kolmykov, 2015-05-11
@dinizzzo

In general, I understand what the problem is. ValueMember and DisplayMember work if $cmbTest.DataSource is set to data of type DataTable and standard output in powershell is of type Array. Because there are no built-in ways to convert an Array to a DataTable, I used the following function:
https://gallery.technet.microsoft.com/scriptcenter...
Accordingly, I changed the first line in my code to:

[System.Data.datatable]$test = Get-ADOrganizationalUnit -filter * -SearchBase "dc=DOMAIN,dc=local" | Out-DataTable

S
Sumor, 2015-05-07
@Sumor

Not too familiar with PowerShell, but in general terms DisplayMember is a property that is output to the combobox instead of ToString(). ValueMember is the name of the property, which can be quickly obtained through the combobox without any additional steps. For example, SelectedValue will immediately return the ValueMember of the selected object.
There is a suspicion that property names are case sensitive. Try Name and distinguishedName

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question