我有一个cboBox,我想用脚本中的表加载。
$dtNetworks = New-Object System.Data.DataTable
[void]$dtNetworks.Columns.Add("Environment")
[void]$dtNetworks.Columns.Add("Name")
[void]$dtNetworks.Columns.Add("Subnet")
[void]$dtNetworks.Rows.Add("Dev","Inside","10.0.00.0/24")
[void]$dtNetworks.Rows.Add("Dev","DMZ","10.0.0.0/24")
[void]$dtNetworks.Rows.Add("QA","Inside","10.1.0.0/24")
[void]$dtNetworks.Rows.Add("QA","DMZ","10.1.0.0/24")
[void]$dtNetworks.Rows.Add("Prod","Inside","10.2.0.0/24")
[void]$dtNetworks.Rows.Add("Prod","DMZ","10.2.0.0/24")
$WPFtxt_dNetwork.DisplayMemberPath = "Name"
$WPFtxt_dNetwork.SelectedValuePath = "Subnet"
#$WPFtxt_dNetwork.ItemsSource = $dtNetworks.DefaultView
$WPFtxt_dNetwork.ItemsSource = $dtNetworks.Select("Environment = 'Dev'")
$WPFtxt_dNetwork.SelectedIndex = 1
$form.Activate()
$ dtNetworks.DefaultView使用$ dtNetworks中的所有值工作并填充cboBox。它还返回一个System.Data.DataRowView类型。
select语句对它自己起作用并只返回Dev值,但它返回一个System.Data.DataRow类型。填写表单时,cboBox中没有条目。
如何将$ dtNetworks的内容过滤到DataRowView?
答案 0 :(得分:3)
不使用.Select()
方法定义源,而是将源保留为DefaultView,并将过滤器应用于该视图。
$dtNetworks = New-Object System.Data.DataTable
[void]$dtNetworks.Columns.Add("Environment")
[void]$dtNetworks.Columns.Add("Name")
[void]$dtNetworks.Columns.Add("Subnet")
[void]$dtNetworks.Rows.Add("Dev","Inside","10.0.00.0/24")
[void]$dtNetworks.Rows.Add("Dev","DMZ","10.0.0.0/24")
[void]$dtNetworks.Rows.Add("QA","Inside","10.1.0.0/24")
[void]$dtNetworks.Rows.Add("QA","DMZ","10.1.0.0/24")
[void]$dtNetworks.Rows.Add("Prod","Inside","10.2.0.0/24")
[void]$dtNetworks.Rows.Add("Prod","DMZ","10.2.0.0/24")
$WPFtxt_dNetwork.DisplayMemberPath = "Name"
$WPFtxt_dNetwork.SelectedValuePath = "Subnet"
$dtNetworks.DefaultView.Filter = "Environment = 'Dev'"
$WPFtxt_dNetwork.ItemsSource = $dtNetworks.DefaultView
$WPFtxt_dNetwork.SelectedIndex = 1
$form.Activate()