editing a WPF DataGrid
-
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
-
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
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