3

I found this question about loading .net 4.0 dll in Powershell.

Now I want to know which Add-Type I have to use, to be able to use the WPF Datagrid from PowerShell ISE

before the following works

[xml] $xaml = @"
<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MainWindow" Height="350" Width="525">
  <DataGrid Height="200" Width="500" HorizontalAlignment="Left" Margin="12,21,0,0"
  Name="McDataGrid" VerticalAlignment="Top" RowHeight="30" ColumnWidth="100"      >
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )

$Form.ShowDialog() 
2
  • There seem to be some examples with WPF toolkit. Commented May 1, 2011 at 9:19
  • Why should anyone install WPF toolkit 3.5, when he has .NET 4.0 ? Both are a bit different Commented May 1, 2011 at 10:43

1 Answer 1

3

It was a combination of multiple errors.

  1. A slash was missing at the end of the tag
  2. I had a TYPO inpowershell_ise.exe.Config and the .Net 4.0 assemblies didn't load
  3. It's a good to check which assemblies are loaded with

    [System.AppDomain]::CurrentDomain.GetAssemblies() | sort location

Now here is a working solution

function Invoke-sql1
{
    param( [string]$sql,
           [System.Data.SQLClient.SQLConnection]$connection
           )
    $cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
    $ds = New-Object system.Data.DataSet
    $da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
    $da.fill($ds) | Out-Null
    return $ds.tables[0].rows
}


[xml] $xaml = @"
<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid 
            AutoGenerateColumns="True" 
            HorizontalAlignment="Left" 
            Name="dataGrid1" 
            VerticalAlignment="Top" 
            Width="330"
            HeadersVisibility="All" 
            >
            <DataGrid.Columns>
                <DataGridTextColumn Header="title"
                                Binding="{Binding title}" 
                                />
                <DataGridTextColumn Header="itemid"
                                Binding="{Binding itemid}" 
                                />
            </DataGrid.Columns>
        </DataGrid >
    </Grid>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )


$con = New-Object System.Data.SqlClient.SqlConnection
$con.ConnectionString = "Data Source=localhost;Initial Catalog=ABDATA;Integrated Security=True"
$con.open()

$sql = @"
SELECT 'abc' title, 3 itemid
union
SELECT 'xyz' title, 2 itemid
union
SELECT 'efg' title, 1 itemid
"@

$dg = $Form.FindName("dataGrid1")
$dg.ItemsSource = @(Invoke-sql1 $sql $con)
$Form.ShowDialog() 

The only monor problem is, that I have to define the columns myself. I thought that could be done automatically.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.