Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. editing a WPF DataGrid

editing a WPF DataGrid

Scheduled Pinned Locked Moved WPF
csharpwpfquestionxmlhelp
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pitwi
    wrote on last edited by
    #1

    Hi! I've searched for a couple of hours here and in other forums and at GitHub but couldn't find an answer. I have a XML file and can read it in a DataTable and show that in a DataGrid. But I want to edit cells/values in the DataGrid and write the corrected values back to the XML file. It's easy for me to write the corrections to the DataTable and back to the XML file but how can I do the edit in the DataGrid ??? What I have so far with VisualStudio 2022:

    using ...

    namespace Gewicht
    {
    /// /// Interaction logic for MainWindow.xaml
    ///
    public partial class MainWindow : Window
    {
    public string[] dbFile = { @"H:\Daten\C#WPF\Gewicht\GewichtDaten.xml", @"H:\Daten\C#WPF\Gewicht\GewichtDaten.xsd" };
    public DataTable dbDataTbl = new DataTable();

        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Window\_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.IO.File.Exists(dbFile\[0\]) | !System.IO.File.Exists(dbFile\[1\]))
                {
                SystemSounds.Beep.Play();
                MessageBox.Show("Die Datei" + Environment.NewLine + "   " + dbFile\[0\] + Environment.NewLine + "und/oder" + Environment.NewLine + "   " + dbFile\[1\] + Environment.NewLine + "fehlt." + Environment.NewLine + "Das Programm wird beendet.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                Environment.Exit(0);
            }
            DataGrid1.CanUserAddRows = false;
            DataGrid1.CanUserDeleteRows = false;
            OpenData();
        }
    
    
        private void DataGrid1\_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            // edit
            // ... ???????????????????????? ...
        }
    
    
        private void OpenData()
        {
            // open data
    
            dbDataTbl.Columns.Clear();
            dbDataTbl.Rows.Clear();
            dbDataTbl = new DataTable("ich");
            dbDataTbl.ReadXmlSchema(dbFile\[1\]);
            dbDataTbl.ReadXml(dbFile\[0\]);
            // DataGrid
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(dbFile\[0\]);
            DataView dataView = new DataView(dataSet.Tables\[0\]);
            DataGrid1.ItemsSource = dataView;
            // format columns
            Style style = new Style();               // creates object of style class   
            style.TargetType = typeof(DataGridCell); // sets target type as Data
    
    L 1 Reply Last reply
    0
    • P pitwi

      Hi! I've searched for a couple of hours here and in other forums and at GitHub but couldn't find an answer. I have a XML file and can read it in a DataTable and show that in a DataGrid. But I want to edit cells/values in the DataGrid and write the corrected values back to the XML file. It's easy for me to write the corrections to the DataTable and back to the XML file but how can I do the edit in the DataGrid ??? What I have so far with VisualStudio 2022:

      using ...

      namespace Gewicht
      {
      /// /// Interaction logic for MainWindow.xaml
      ///
      public partial class MainWindow : Window
      {
      public string[] dbFile = { @"H:\Daten\C#WPF\Gewicht\GewichtDaten.xml", @"H:\Daten\C#WPF\Gewicht\GewichtDaten.xsd" };
      public DataTable dbDataTbl = new DataTable();

          public MainWindow()
          {
              InitializeComponent();
          }
      
          private void Window\_Loaded(object sender, RoutedEventArgs e)
          {
              if (!System.IO.File.Exists(dbFile\[0\]) | !System.IO.File.Exists(dbFile\[1\]))
                  {
                  SystemSounds.Beep.Play();
                  MessageBox.Show("Die Datei" + Environment.NewLine + "   " + dbFile\[0\] + Environment.NewLine + "und/oder" + Environment.NewLine + "   " + dbFile\[1\] + Environment.NewLine + "fehlt." + Environment.NewLine + "Das Programm wird beendet.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                  Environment.Exit(0);
              }
              DataGrid1.CanUserAddRows = false;
              DataGrid1.CanUserDeleteRows = false;
              OpenData();
          }
      
      
          private void DataGrid1\_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
          {
              // edit
              // ... ???????????????????????? ...
          }
      
      
          private void OpenData()
          {
              // open data
      
              dbDataTbl.Columns.Clear();
              dbDataTbl.Rows.Clear();
              dbDataTbl = new DataTable("ich");
              dbDataTbl.ReadXmlSchema(dbFile\[1\]);
              dbDataTbl.ReadXml(dbFile\[0\]);
              // DataGrid
              DataSet dataSet = new DataSet();
              dataSet.ReadXml(dbFile\[0\]);
              DataView dataView = new DataView(dataSet.Tables\[0\]);
              DataGrid1.ItemsSource = dataView;
              // format columns
              Style style = new Style();               // creates object of style class   
              style.TargetType = typeof(DataGridCell); // sets target type as Data
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Instead of a DataTable, deserialize the XMl into a collection of classes, that implement INotifyPropertyChanged, and bind to that. [Details of XML serialization | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/standard/serialization/introducing-xml-serialization) [https://stackoverflow.com/questions/4203540/generate-c-sharp-class-from-xml\](https://stackoverflow.com/questions/4203540/generate-c-sharp-class-from-xml)

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups