Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could listen to the event for when the cell ends the edit and then save your data source. i.e. place this in your constructor of the control that hosts the grid (probably a user control, window, page etc) after the InitializeComponent() call</p> <pre><code> this.myDataGrid.CellEditEnding += new EventHandler&lt;DataGridCellEditEndingEventArgs&gt;(grid_CellEditEnding); </code></pre> <p>and then have the handler save the data source</p> <pre><code> void grid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { //save my data source } </code></pre> <p>personally i prefer the approach where you can perform your edits and then hit save at the end, but in your case you could use String.Join to create a CSV String and then write it to a file. </p> <p>To do this, make a property which is your List, so your building of your data for the grid would look like this:</p> <pre><code>public Collection&lt;Person&gt; MyPersonDataSource {get; private set; } public MyWindowsConstructor() { //build the grid data before you initialize the window, as the PersonDataSource //does not implement NotifyPropertyChanged, if you build the data afterwards //the binding won't be updated BuildGridData(); InitializeComponent(); } private void BuildGridData(){ this.MyPersonDataSource = new Collection&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; this.MyPersonDataSource.Add(pers); } } </code></pre> <p>then in your cell end edit function</p> <pre><code> void grid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { //save my data source var nameArray = this.MyPersonDataSource.Select(item =&gt; item.Name).ToArray(); //create the csv string String csvString = String.Join("," nameArray); //write it to a file System.IO.File.WriteAllText(@"C:\SomeFolderYouHavePermissionsOn\names.csv", csvString); } </code></pre> <p>I would bind my grid straight to the property MyPersonDataSource, like so..</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" Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=MyPersonDataSource}" 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; &lt;/Window&gt; </code></pre> <p>And i would probably look at more robust data storage than CSV, you can use xml and bind to it using XPath, but I haven't used this enough to frame an appropriate answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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