用CSV显示成员填充组合框 [英] Fill combobox with csv displaymember
本文介绍了用CSV显示成员填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的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
这篇关于用CSV显示成员填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文