Jason
Jason

Reputation: 821

powershell: combobox only displays System.Data.DataRowView

Is there something else I need to add in order to connect the DataTable to the ComboBox or have it list the items?

Currently, it will show items, but they all are System.Data.DataRowView and I am not sure why.

<ComboBox x:Name="WPFDomainUsersBox" HorizontalAlignment="Left" Margin="288,10,0,0" VerticalAlignment="Top" Width="215" Height="23" Text="Domain Users"/>
...
...
...
#create a datatable to bind to our combobox
    $datatable = New-Object system.Data.DataTable
    #Define Columns
    $ColValue = New-Object system.Data.DataColumn "Value",([string])
    $ColText = New-Object system.Data.DataColumn "Text",([string])
    #add columns to datatable
    $datatable.columns.add($ColValue)
    $datatable.columns.add($ColText)

    #List option.
    $DomainUsers = Get-ADUser -Filter *
    ForEach($DUsers in $DomainUsers) {
        #$WPFDomainUsersBox.Items.Add($DUsers.SamAccountName)

        $datarow = $datatable.NewRow()
        #Enter data in the row
        $datarow.Value = $DUsers.SamAccountName
        $datarow.Text = $DUsers.SamAccountName
        #Add the row to the datatable
        $datatable.Rows.Add($datarow)
    }

    $WPFDomainUsersBox.ItemsSource = $datatable.DefaultView

Upvotes: 1

Views: 427

Answers (1)

mm8
mm8

Reputation: 169370

Set the DisplayMemberPath property of the ComboBox to the name of the column in the DataTable that you want to display:

$WPFDomainUsersBox.DisplayMemberPath = "Text"

Upvotes: 3

Related Questions