Page 1 of 1

import-csv and combobox

Posted: Thu Jul 26, 2018 7:12 am
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

Re: import-csv and combobox

Posted: Thu Jul 26, 2018 7:24 am
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.

Re: import-csv and combobox

Posted: Fri Aug 03, 2018 1:24 pm
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...

Re: import-csv and combobox

Posted: Fri Aug 03, 2018 1:38 pm
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.

Re: import-csv and combobox

Posted: Fri Aug 03, 2018 1:41 pm
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.