Data Loading into my Listbox does not look correct

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.
This topic is 10 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
j0ej0e21
Posts: 11
Last visit: Tue Jun 06, 2017 11:30 am

Data Loading into my Listbox does not look correct

Post by j0ej0e21 »

Hi there,

I am fairly new to Powershell and Powershell Studio. I am writing a script that connects out to a SQL database, and then returns a Dataset, and then I am trying to use a piece of this dataset to populate a list box, but instead of the nice computer names, I am getting @{Name=ComputerName} for each line instead of just Computer Name

SQL Connection Code
SQLConnection.jpg
SQLConnection.jpg (100.05 KiB) Viewed 7298 times

Using the Dataset Data to fill Listbox
AddingData.jpg
AddingData.jpg (80 KiB) Viewed 7298 times

If someone could help with why I am getting this result, I would appreciate it!

Results
Results.jpg
Results.jpg (77.43 KiB) Viewed 7298 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Data Loading into my Listbox does not look correct

Post by jvierra »

Don't use select at teh end of teh where filter.

Call the listbox like this;

Load-ListBox $Listbox1 $names 'Name'

DO not enumerate in a for just hand the data set.

Listboxes are data capable so you can just assign the datatable as the data source and use the listbox data specs to assign the field to diaplay and teh field to return,
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Data Loading into my Listbox does not look correct

Post by jvierra »

Here is code I tested.

$listbox1.DataSource=$dataset.Tables[0]
$listbox1.DisplayMember='Name'
$listbox1.ValueMember='guid

The ValueMember can be any field in the table. That is what the listbox returns as the '$listbox1.SelectedValue'. This is what allows a listbox to be a lookup control. It can doisplay plain text names like a users full name and return the accountid or email address. In this case I have set it to return the GUID.

We can also easily leverage this to drive a filter for anotherlistbox.
User avatar
j0ej0e21
Posts: 11
Last visit: Tue Jun 06, 2017 11:30 am

Re: Data Loading into my Listbox does not look correct

Post by j0ej0e21 »

jvierra wrote:Don't use select at teh end of teh where filter.

Call the listbox like this;

Load-ListBox $Listbox1 $names 'Name'

DO not enumerate in a for just hand the data set.

Listboxes are data capable so you can just assign the datatable as the data source and use the listbox data specs to assign the field to diaplay and teh field to return,
Thanks for the Help! Adding 'Name' to the send of my Load-Listbox command worked! However, when I tried to remove the Select at the end of the $dataset gather, this still did not work, because all I pulled back then is a bunch of rows that showed "System.Data.DataRow"
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Data Loading into my Listbox does not look correct

Post by jvierra »

It is more efficient to do the filtering in the select statement.
User avatar
j0ej0e21
Posts: 11
Last visit: Tue Jun 06, 2017 11:30 am

Re: Data Loading into my Listbox does not look correct

Post by j0ej0e21 »

If I do a command to load the datasource into the Listbox - example...

$listbox1.datasource = $dataset.Tables[0]

How would I then filter out what I need from the $dataset to display into the Listbox?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Data Loading into my Listbox does not look correct

Post by jvierra »

select * from table WHERE column = 'value'

Just use sql to filter. It would be faster.
This topic is 10 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked