Author Posts

April 3, 2016 at 3:06 am

Hi all,

Unfortunately I'm double posting this item on two websites, sorry for that but I realy need this to work and I'm breaking my head on this matter.
Link to Stackoverflow: http://stackoverflow.com/questions/36254437/powershell-save-edited-datagrid-to-xml-file

Below is my latest code, the link above is for refference.

I've created a dataset and this set is showed in the grid. The only thing that still doesn't work, is save the grid to a variable or directly to the XML file when the button is pressed. And I hope someone could tell or show me how I can get the values of the datagrid back to the dataset and save the set in the XML file. I know that saving the set could be done trough $dt.WriteXML("xmlfile.xml")

Link to XML and WPF XAML Pastebin

Script to populate grid with dataset

$dt = New-Object "System.Data.DataTable" "DatacopyGrid"
    
[void]$dt.Columns.Add("Source")
[void]$dt.Columns.Add("Destination")
    
ForEach ($Row in $Setting.Setting.Datacopy.Row) { $dt.Rows.Add(@($Row.Source,$Row.Destination)) }
    
$DatacopyGrid.ItemsSource = $dt.DefaultView
$DatacopyGrid.CanUserAddRows = $True
$DatacopyGrid.CanUserDeleteRows = $True 
$DatacopyGrid.IsReadOnly = $False
    
$DatacopyBtnStart.Add_Click({
    $DatacopyGrid.Rows | select -expand DataBoundItem
})

I've tried to pull the values from the grid with:

$dt.Items | %{
    $_.Subitems[1].Text
    $_.Subitems[1].Text
    $_.Subitems[2].Text
    $_.Subitems[2].Text
}

$dt.Tables["DatacopyGrid"].Rows[0].Source
$dt.Tables["DatacopyGrid"].Rows[0].Destination
$dt.Tables["DatacopyGrid"].Rows[1].Source
$dt.Tables["DatacopyGrid"].Rows[1].Destination
$dt.Tables["DatacopyGrid"].Rows[2].Source
$dt.Tables["DatacopyGrid"].Rows[2].Destination

But non of the variables return anything...

Hope to hear and thanks again!

Regards, Paul

April 14, 2016 at 1:26 am

Nobody that can help?

April 14, 2016 at 2:52 am

PS D:\> $dt = New-Object "System.Data.DataTable" "DatacopyGrid"
PS D:\> [void]$dt.Columns.Add("Source")
PS D:\> [void]$dt.Columns.Add("Destination")
PS D:\> [void]$dt.Rows.Add('S1','D1')
PS D:\> [void]$dt.Rows.Add('S2','D2')
PS D:\> $dt

Source Destination
------ -----------
S1     D1
S2     D2
PS D:\> $dt.Rows[0]

Source Destination
------ -----------
S1     D1

PS D:\> $dt.Rows[1]

Source Destination
------ -----------
S2     D2

PS D:\> $dt.Rows[1].Source
S2
PS D:\> $dt.Rows[1].Destination
D2

DataTable class doesn't have Items or Tables property
RTFM, pls 🙂
https://msdn.microsoft.com/en-us/library/system.data.datatable(v=vs.110).aspx

April 14, 2016 at 7:35 am

Hi @Max Kozlov,

I'm just a beginner and the datatable was just a hopeless try to get the data returned after I pushed the Ok button. I've tried at least three differtent ways to get the data out. And I can't get it to work. This thing is bugging me for three weeks now...

The thing I'm trying to create is a object in the $button.add_click{} that can be returned to the script later on so that I can get the values and save them to the XML file.

Thanks for your reply though 🙂