Home Forums WPF controls Xceed DataGrid for WPF Answer to getting current cell value before row changes

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #22793 |

    The datagrid doesn’t update the Content of a cell until the row is done editing(I think thats when it happens). Mostly seeing problems with cell value not current until I click of the row then back on.

    So here is a set of derived classes with a few added features(for my purposes in hierarchical data)

    You can cast the cells to ICustomCell to get the CellValue which is the current data for the cell not yet set on Content.

    Imported from legacy forums. Posted by MiddleTommy (had 4952 views)

    User (Old forums)
    Member
    Post count: 23064

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Xceed.Wpf.DataGrid;
    using Xceed.Wpf.DataGrid.ValidationRules;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows;
    using System.Windows.Input;
    using System.Globalization;
    using System.Timers;

    namespace MyCards.Data.Tools
    {

    public class CustomInsertionRow : InsertionRow
    {
    static CustomInsertionRow()
    {
    EventManager.RegisterClassHandler(typeof(CustomInsertionRow), CustomInsertionCell.InsertionCellEndedEditEvent, new RoutedEventHandler(OnInsertionCellEndedEdit));
    }

    protected override Cell CreateCell(Column column)
    {
    return new CustomInsertionCell();
    }

    private static void OnInsertionCellEndedEdit(object sender, RoutedEventArgs e)
    {
    e.Source = sender;
    }
    }

    public class CustomDataRow : DataRow
    {
    static CustomDataRow()
    {
    EventManager.RegisterClassHandler(typeof(CustomDataRow), CustomDataCell.CellEndedEditEvent, new RoutedEventHandler(OnCellEndedEdit));
    }

    protected override Cell CreateCell(Column column)
    {
    return new CustomDataCell();
    }

    private static void OnCellEndedEdit(object sender, RoutedEventArgs e)
    {
    e.Source = sender;
    }
    }

    public class CustomInsertionCell : InsertionCell,ICustomCell
    {
    public static readonly RoutedEvent InsertionCellEndedEditEvent =
    EventManager.RegisterRoutedEvent(“InsertionCellEndedEdit”, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CustomInsertionCell));

    public CustomInsertionCell()
    {
    Binding ceb = new Binding(“ActualContent”);
    ceb.Source = this;
    ceb.Mode = BindingMode.TwoWay;
    BindingOperations.SetBinding(this, CustomInsertionCell.CellValueProperty, ceb);
    }

    public static readonly DependencyProperty CellValueProperty =
    DependencyProperty.Register(“CellValue”, typeof(object), typeof(CustomInsertionCell), new UIPropertyMetadata(null));

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
    base.OnPropertyChanged(e);
    if (e.Property == CustomInsertionCell.IsBeingEditedProperty)
    {
    if ((bool)(e.NewValue) == false)
    {
    this.RaiseEvent(new RoutedEventArgs(CustomInsertionCell.InsertionCellEndedEditEvent));
    }
    }
    }

    public object CellValue
    {
    get
    {
    return (object)this.GetValue(CustomInsertionCell.CellValueProperty);
    }
    set
    {
    oldContent = (object)this.GetValue(CustomInsertionCell.CellValueProperty);
    this.SetValue(CustomInsertionCell.CellValueProperty, value);
    }
    }
    private object oldContent = null;
    public object OldCellValue
    {
    get
    {
    return oldContent;
    }
    set
    {
    oldContent = value;
    }
    }

    }

    public class CustomDataCell : DataCell,ICustomCell
    {
    public static readonly RoutedEvent CellEndedEditEvent =
    EventManager.RegisterRoutedEvent(“CellEndedEdit”, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CustomDataCell));

    public CustomDataCell()
    {
    Binding ceb = new Binding(“ActualContent”);
    ceb.Source = this;
    ceb.Mode = BindingMode.TwoWay;
    BindingOperations.SetBinding(this, CustomDataCell.CellValueProperty, ceb);
    }

    public static readonly DependencyProperty CellValueProperty =
    DependencyProperty.Register(“CellValue”, typeof(object), typeof(CustomDataCell), new UIPropertyMetadata(null));

    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
    base.OnPropertyChanged(e);
    if (e.Property == CustomDataCell.IsBeingEditedProperty)
    {
    if ((bool)(e.NewValue) == false)
    {
    this.RaiseEvent(new RoutedEventArgs(CustomDataCell.CellEndedEditEvent));
    }
    }
    }

    public object CellValue
    {
    get
    {
    return (object)this.GetValue(CustomDataCell.CellValueProperty);
    }
    set
    {
    oldContent = (object)this.GetValue(CustomInsertionCell.CellValueProperty);
    this.SetValue(CustomDataCell.CellValueProperty, value);
    object o = this.Content;
    }
    }
    private object oldContent = null;
    public object OldCellValue
    {
    get
    {
    return oldContent;
    }
    set
    {
    oldContent = value;
    }
    }
    }

    public delegate void ItemChangedHandler(object selecteditem);
    public delegate void VoidHandler();

    public class CustomDataGridControl : DataGridControl
    {
    private const string SELECTEDITEM = “SelectedItem”;
    private const string CURRENTITEM = “CurrentItem”;
    public event ItemChangedHandler SelectedItemChanged;
    private bool arrowDown;
    private Timer arrowTimer;

    public bool ArrowDown

    Imported from legacy forums. Posted by MiddleTommy (had 876 views)

    User (Old forums)
    Member
    Post count: 23064

    I uses this all the time in Cell validation when I have to Validate a cell to other cells data or Change other cells data based on the current cells data.

    Note: OldCellValue is not 100% only works when a cell value has been set through CellValue.
    Usefull if you want to do something only if the data is different.

    Imported from legacy forums. Posted by MiddleTommy (had 797 views)

    User (Old forums)
    Member
    Post count: 23064

    When doing validation I like to set the CellValue to visually update the Cell
    and also set the cellValidationContext.DataItem to the current value so the source is updated incase the grid hasnt updated it yet when I access it(dont know if that is necessary but it makes me feel warm and fuzzy)

    (DataItem is the rows datasource not the cells)

    Imported from legacy forums. Posted by MiddleTommy (had 595 views)

    User (Old forums)
    Member
    Post count: 23064

    I’m newbie in C# so please show how to get just edited cell value. For example how can I get current cell value in MessageBox.Sting(CellValue.ToString());
    Please.

    Imported from legacy forums. Posted by fluxay (had 643 views)

    User (Old forums)
    Member
    Post count: 23064

    How to get simply cell value using
    MessageBox.Show(CellValue.ToString());

    Imported from legacy forums. Posted by fluxay (had 611 views)

    Xceed Support
    Member
    Post count: 5658

    Assuming that the underlying data source is a DataTable:

    ( ( System.Data.DataRow )this.myGrid.CurrentItem )[ 0 ].ToString();

    If you want to use the Xceed.Wpf.DataGrid.DataRow container:

    Xceed.Wpf.DataGrid.DataRow container = this.myGrid.GetContainerFromItem( this.myGrid.CurrentItem ) as Xceed.Wpf.DataGrid.DataRow;

    if( container != null )
    {
    container.Cells[ 0 ].Content.ToString();
    }

    Imported from legacy forums. Posted by Jenny [Xceed] (had 5871 views)

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.