import-csv and combobox

Ask your Windows 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.
Locked
isteph26
Posts: 1
Joined: Thu Jul 26, 2018 7:10 am

import-csv and combobox

Post by isteph26 » Thu Jul 26, 2018 7:12 am

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

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: import-csv and combobox

Post by jvierra » Thu Jul 26, 2018 7:24 am

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
Joined: Fri Aug 03, 2018 7:02 am

Re: import-csv and combobox

Post by thatsameer » 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...

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: import-csv and combobox

Post by jvierra » Fri Aug 03, 2018 1:38 pm

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.

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: import-csv and combobox

Post by jvierra » Fri Aug 03, 2018 1:41 pm

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.

Locked