Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 961583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:22:06+00:00 2026-05-16T01:22:06+00:00

I have a datagrid in a WPF populated from a csv file. This is

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:22:07+00:00Added an answer on May 16, 2026 at 1:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble pasting from a csv into the wpf datagrid - I have
I have a datagrid which is populated with CSV data when the user drag/drops
I currently have a WPF DataGrid binded to a DataSet via the DataGrid's ItemsSource
I have WPF DataGrid (VS2010 C#). I copied the data from DataGrid to Clipboard
I have a datagrid, populated as shown below. When the user clicks on a
I have a DataGrid, populated with objects in an ArrayCollection. After updating one of
I have a DataGrid (WPF Toolkit) with a custom combobox like edit template of
I have a problem with my WPF application. I have a datagrid (Wpf Toolkit),
I have wpf datagrid, in one of columns, I checkboxes added in it, now
I have a WPF Datagrid binded with list of interface objects. Consider, ClsEmployee class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.