I have a datagrid in a WPF populated from a csv file. This is done with Linq and populating the datagrid via CollectionViewSource.Source.
The requirement is for any updates/changes made to the data in the datagrid to then be saved back into the csv.
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.
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.
System.Windows.Data.CollectionViewSource personViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("personViewSource")));
List<Person> T = new List<Person>();
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;
The xaml:
<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">
<Window.Resources>
<CollectionViewSource x:Key="personViewSource" d:DesignSource="{d:DesignInstance my:Person, CreateList=True}" />
</Window.Resources>
<StackPanel Width="369" Height="230" DataContext="{StaticResource personViewSource}">
<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">
<DataGrid.Columns>
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name, Mode=TwoWay, NotifyOnTargetUpdated=True}" Header="Name" Width="SizeToHeader" />
<DataGridTextColumn x:Name="ageColumn" Binding="{Binding Path=Age}" Header="Age" Width="SizeToHeader" Foreground="#FFC14040" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
Thanks