tags:

views:

54

answers:

1

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

+1  A: 

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

   this.myDataGrid.CellEditEnding += new EventHandler<DataGridCellEditEndingEventArgs>(grid_CellEditEnding);

and then have the handler save the data source

  void grid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
     //save my data source
  }

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.

To do this, make a property which is your List, so your building of your data for the grid would look like this:

public Collection<Person> 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<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;
     this.MyPersonDataSource.Add(pers);
  }
}

then in your cell end edit function

  void grid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
     //save my data source
     var nameArray = this.MyPersonDataSource.Select(item => 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);
  }

I would bind my grid straight to the property MyPersonDataSource, like so..

<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" Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=MyPersonDataSource}" 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>
</Window>

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.

Aran Mulholland
How do I save the data source?
Chris
Aran thanks. Could you explain how to bind it straight to the myPersonDataSource?If I convert all the csv's to xml is there a better way still?
Chris