import-csv and combobox

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.
This topic is 5 years and 7 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
isteph26
Posts: 1
Last visit: Tue Sep 04, 2018 5:30 am

import-csv and combobox

Post by isteph26 »

Hi all
I need your help

I've got a csv file like this:

company;site
company1;site1,site2
company2;site3,site4

and 2 comboxBox

comboBox1 => company
comboBox2 => site

I want import my csv file to list in ComboBox 1 the list of company (company1 company2)

so I use :
import-csv ".source.csv" -delimiter ";" | %{$comboBox1.Items.Add($_.company)}

and accordin to the choose company I want to list in ComboBox2 the list of site of this company

for example comboBox1 = company2, I want in comboBox2 = site3 site4 (one below the other)

Thanks for your help
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: import-csv and combobox

Post by jvierra »

You have to prep the data and add it to the box. Normally we add all data to the first then when the selection changes pule the selected row and fill the second box from the row in the first box.

Give it a try and post back.
thatsameer
Posts: 18
Last visit: Thu Apr 18, 2019 7:07 am

Re: import-csv and combobox

Post by thatsameer »

Try this my friend, add to the form load section of your script:

$csvlist = import-csv -path 'C:\path to your csv file'

$combobox1.DataSource = [system.Collections.ArrayList]$csvlist
$combobox1.DisplayMember = 'company'

$combobox2.DataSource = [system.Collections.ArrayList]$csvlist
$combobox2.DisplayMember = 'site'

Let me know if it works.

I'm aware my code is more than 3 lines, I really don't understand how adding the code block works... sorry... just joined today...
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: import-csv and combobox

Post by jvierra »

thatsameer wrote: Fri Aug 03, 2018 1:24 pm Try this my friend, add to the form load section of your script:

$csvlist = import-csv -path 'C:\path to your csv file'

$combobox1.DataSource = [system.Collections.ArrayList]$csvlist
$combobox1.DisplayMember = 'company'

$combobox2.DataSource = [system.Collections.ArrayList]$csvlist
$combobox2.DisplayMember = 'site'

Let me know if it works.

I'm aware my code is more than 3 lines, I really don't understand how adding the code block works... sorry... just joined today...
Unfortunately that won't work. The site has to be dependent on the company. We can use a dtatable and a view to get the relation or we can store the sites in CB 1.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: import-csv and combobox

Post by jvierra »

Combo2 for this CSV would look like this


$list = Combobox1.SelectedItem.Sites -split ';'
$combobox2.Items.Clear()
$combobox2.Items.AddRange($list)


All of this would happen on the CB1 "SelectedIndexChanged" event.
This topic is 5 years and 7 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