Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Update from datagrid
    text
    copied!<p>I have a datagrid in a WPF populated from a csv file. This is done with Linq and populating the datagrid via CollectionViewSource.Source.</p> <p>The requirement is for any updates/changes made to the data in the datagrid to then be saved back into the csv.</p> <p>I need to know how to save any changes to the data? I've been playing around with some of the events and datacontext's and such like but nothing works yet.</p> <p>I apologise if this is a beginner's type question. The move from windows applications to WPF is a steep learning curve(for me at least). I've just populated from an array for now while I try and figure it out. Basically just want to get the data back out again, save it as var.</p> <pre><code> System.Windows.Data.CollectionViewSource personViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("personViewSource"))); List&lt;Person&gt; T = new List&lt;Person&gt;(); Person p = new Person(); string[] str = new string[] { "Stacey", "Olivia", "Dylan", "Lauryn", "Beth", "Caitlin" }; var data = from s in str select s; Person pers; foreach (var d in data) { pers = new Person(); pers.Name = d; pers.Age = 22; T.Add(pers); } personViewSource.Source = T; </code></pre> <p>The xaml: </p> <pre><code>&lt;Window x:Class="WpfApplication4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Name="win1" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:WpfApplication4"&gt; &lt;Window.Resources&gt; &lt;CollectionViewSource x:Key="personViewSource" d:DesignSource="{d:DesignInstance my:Person, CreateList=True}" /&gt; &lt;/Window.Resources&gt; &lt;StackPanel Width="369" Height="230" DataContext="{StaticResource personViewSource}"&gt; &lt;DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Name="personDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Width="88" HorizontalAlignment="Left" BorderThickness="4" Background="#FFF8C5C5" SelectionChanged="personDataGrid_SelectionChanged" TextInput="personDataGrid_TextInput" RowEditEnding="personDataGrid_RowEditEnding" TargetUpdated="personDataGrid_TargetUpdated"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name, Mode=TwoWay, NotifyOnTargetUpdated=True}" Header="Name" Width="SizeToHeader" /&gt; &lt;DataGridTextColumn x:Name="ageColumn" Binding="{Binding Path=Age}" Header="Age" Width="SizeToHeader" Foreground="#FFC14040" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;/StackPanel&gt; </code></pre> <p></p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload