0

Now I've searched for hours but didn't find an answer. I have a WPF app with a DataGrid and want to edit data directly within a DataGrid cell. I can read the Data before change but not after change and therefore I can't save the changes.

And the second question is, how can I format numbers ("0.0") in a DataGrid column?

What I have so far:

There's nothing special in the XAML, only the raw definition of the DataGrid, without column definitions or something else.

And this is my source:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    if (!System.IO.File.Exists(dbFile[0]) | !System.IO.File.Exists(dbFile[1]))  // the 2 XML data files
    {
        SystemSounds.Beep.Play();
        MessageBox.Show("File(s)" + Environment.NewLine + "   " + dbFile[0] + Environment.NewLine + "and/or" + Environment.NewLine + "   " + dbFile[1] + Environment.NewLine + "don't exist." + Environment.NewLine + "The program will be closed.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        Environment.Exit(0);
    }
    OpenData();
}

    private void OpenData()
    {
        DataTable dbDataTbl = new DataTable("MyData");  // name must be the same as in XML!!!
        dbDataTbl.ReadXmlSchema(dbFile[1]);
        dbDataTbl.ReadXml(dbFile[0]);

        // DataGrid
        DataView dataView = new DataView(dbDataTbl);
        DataGrid1.ItemsSource = dataView;
        // format column ID
        DataGrid1.Columns[0].IsReadOnly = true;  // ID
        Style style = new Style();               // creates object of style class   
        style.TargetType = typeof(DataGridCell); // sets target type as DataGrid cell
        Setter s = new Setter();                 // create objects of setter class   
        s.Property = HorizontalAlignmentProperty;
        s.Value = HorizontalAlignment.Right;
        style.Setters.Add(s);
        s = new Setter();
        s.Property = ForegroundProperty;
        s.Value = Brushes.LightGray;
        style.Setters.Add(s);
        DataGrid1.Columns[0].CellStyle = style;
        // format column Date
        style = new Style();
        style.TargetType = typeof(DataGridCell);
        s = new Setter();
        s.Property = ContentStringFormatProperty;
        s.Value = "yyyy-MM-dd";                   // DOES NOT WORK !!!
        style.Setters.Add(s);
        DataGrid1.Columns[1].CellStyle = style;
        // format column Weight
        style = new Style();
        style.TargetType = typeof(DataGridCell);
        s = new Setter();
        s.Property = HorizontalAlignmentProperty;
        s.Value = HorizontalAlignment.Right;
        style.Setters.Add(s);
        s = new Setter();
        s.Property = ContentStringFormatProperty;
        //s.Value = "{}{0:0.0}";                   // DOES NOT WORK !!!
        s.Value = "0.0";                           // DOES NOT WORK !!!
        style.Setters.Add(s);
        DataGrid1.Columns[2].CellStyle = style;
        // format column Text
        DataGrid1.Columns[3].MinWidth = 115;
    }

    private void DataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        int row = DataGrid1.Items.IndexOf(DataGrid1.CurrentItem);
        int col = DataGrid1.CurrentCell.Column.DisplayIndex;
        DataRowView drv = (DataRowView)DataGrid1.SelectedItem;
        int id = Convert.ToInt32(drv.Row[0]);            // value of ID
        string val = drv.Row.ItemArray[col].ToString();  // old value of a cell
        if (e.EditAction == DataGridEditAction.Commit)
        {
            var t = e.EditingElement as TextBox;  // assumes columns are all TextBoxes
            var txt = t.Text.ToString();
            // when I press Enter after editing cell data I always get the old data,
            // never the edited new one

            // save data
            // ...
        }
    }

thanks

Hein Kurz
  • 1
  • 1

0 Answers0