用CSV显示成员填充组合框

人气:329 发布:2022-10-16 标签: csv powershell combobox bind

问题描述

我创建了一个简单的Windows表单和一个组合框,我试图用CSV中的Full name列填充它,并且我希望它在一个变量中返回缩写的name列值,所以我使用了一个哈希表并添加了这两个列,但从组合框中选择一个项后,$namehash[$Return]为空。难道它不应该退还被删减的名字吗?我在C#和VB中找到了使用Combox.DisplayMember、Combox.ValueMember、Combox.DataSource的其他示例。如何在PowerShell中执行此操作?

我的CSV用逗号分隔

名称、简称

密歇根州

俄亥俄州

Import-Module Activedirectory


function button ($WF) {

###################Load Assembly for creating form & button######

[void][System.Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.VisualBasic")


#####Define the form size & placement

$form = New-Object "System.Windows.Forms.Form";
$form.Width = 500;
$form.Height = 190;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$form.ControlBox = $True


##############Define text label2

$textLabel2 = New-Object "System.Windows.Forms.Label";
$textLabel2.Left = 25;
$textLabel2.Top = 80;

$textLabel2.Text = $WF;


############Define text box2 for input

$cBox2 = New-Object "System.Windows.Forms.combobox";
$cBox2.Left = 150;
$cBox2.Top = 80;
$cBox2.width = 200;


###############"Add descriptions to combo box"##############
 $NameHash = @{}
import-csv 'C:	emp	est_cidb.csv' | ForEach-Object {
    $cBox2.Items.Add($_.Description)
    $NameHash.Add($_.Name,$_.Shortname)
}

############Define text box3 for input

#############Define default values for the input boxes
$defaultValue = ""
$cBox2.Text = $defaultValue;
#############define OK button
$button = New-Object "System.Windows.Forms.Button";
$button.Left = 360;
$button.Top = 45;
$button.Width = 100;
$button.Text = "Ok";
$Button.Cursor = [System.Windows.Forms.Cursors]::Hand
$Button.Font = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::BOLD)
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$cBox2.Text;
$form.Close();};
$button.Add_Click($eventHandler) ;

#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel2);
$form.Controls.Add($cBox2);
$ret = $form.ShowDialog();

#################return values

return $cbox2.text

}

$return= button "Job Descriptions"

我解决了自己的问题,使用它作为数据源创建了一个DataTable,并使用DisplayMember、Valemember、DataSource类绑定了组合框。

$reader = new-object System.IO.StreamReader($csvfile) 
$line = $reader.ReadLine() 
$columns =  $line.Split($csvdelimiter) 

foreach ($column in $columns) {$null = $datatable.Columns.Add($column) }
   while (($line = $reader.ReadLine()) -ne $null)  { 
        $row = $datatable.NewRow() 
        $row.itemarray = $line.Split($csvdelimiter) 
        $datatable.Rows.Add($row)   
}
$cbox2.DisplayMember = "Name"
$cbox2.ValueMember = "Shortname"
$cbox2.DataSource = $datatable

$States=cbox2.SelectedValue

推荐答案

$reader = new-object System.IO.StreamReader($csvfile) 
$line = $reader.ReadLine() 
$columns =  $line.Split($csvdelimiter) 

foreach ($column in $columns) {$null = $datatable.Columns.Add($column) }
   while (($line = $reader.ReadLine()) -ne $null)  { 
        $row = $datatable.NewRow() 
        $row.itemarray = $line.Split($csvdelimiter) 
        $datatable.Rows.Add($row)   
}
$cbox2.DisplayMember = "Name"
$cbox2.ValueMember = "Shortname"
$cbox2.DataSource = $datatable

$States=cbox2.SelectedValue

995