Save edited datagrid to xml file

This topic contains 3 replies, has 2 voices, and was last updated by  Paul_dH 2 years, 2 months ago.

  • Author
  • #37178


    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:

    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"
    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
        $DatacopyGrid.Rows | select -expand DataBoundItem

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

    $dt.Items | %{

    But non of the variables return anything...

    Hope to hear and thanks again!

    Regards, Paul

  • #37723


    Nobody that can help?

  • #37730

    Max Kozlov
    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
    PS D:\> $dt.Rows[1].Destination

    DataTable class doesn't have Items or Tables property
    RTFM, pls 🙂

  • #37771


    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 🙂

You must be logged in to reply to this topic.