Skip to content

Instantly share code, notes, and snippets.

@t2psyto
Created March 27, 2024 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t2psyto/e36752e7cc32f28ee3540634d26eedc1 to your computer and use it in GitHub Desktop.
Save t2psyto/e36752e7cc32f28ee3540634d26eedc1 to your computer and use it in GitHub Desktop.
show in gridview
$header = @("名前", "ほげ", "ふが")
$data = @(("name1", "hoge1", "fuga1"), ("name2", "hoge2", "fuga2"))
function showindatagrid($data, $header) {
Add-Type -AssemblyName PresentationFramework
# XAMLからウインドウ作成
[xml]$xaml = @'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DataGrid表示サンプル" Height="300" Width="400">
<StackPanel>
<DataGrid Name="grdData" CanUserAddRows="False" />
</StackPanel>
</Window>
'@
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
# DataTable作成
$dt = New-Object System.Data.DataTable
$header | %{ $dt.Columns.Add($_) } | Out-Null
$data | %{ $dt.Rows.Add($_) } | Out-Null
# データバインド
$grid = $window.FindName("grdData")
$grid.ItemsSource = $dt.DefaultView
# 表示
$window.ShowDialog()
}
showindatagrid $data $header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment