C1DataGrid: mark edited and new rows

Posted by: firelex on 26 October 2021, 5:47 am EST

    • Post Options:
    • Link

    Posted 26 October 2021, 5:47 am EST

    Hello!

    I have a C1DataGrid bound to a DataTable. Changes are buffered in the grid and are saved only when user presses “Save”-button.

    Thats why I need is to mark new and edited rows by colouring row’s header.

    I’ve managed to mark edited rows in the event handler of the “CommittedRowEdit”-event:

    
    if (_dataTable.Rows(e.Row.Index-1).RowState == DataRowState.Modified) {
      e.Row.HeaderPresenter.Background = new SolidColorBrush(...);
    }
    

    It’s a bit clumsy using internal variable holding data, but e.Row object doesn’t have its state - modified or not. And e.Row.DataItem doesn’t have it either.

    But I cannot use this way for the new rows - when handling this event is row’s HeaderPresenter not yet loaded. On the other hand catching LoadedRowHeaderPresenter-event also doesn’t do, thus all the rows send it when loading and after adding a new row.

    Is there perhaps another way to do this?

    Regards,

    firelex

  • Posted 27 October 2021, 5:24 am EST

    Hi,

    1. The code you have provided is best approach for changing RowHeader’s Background after committed row edit.

    2. For New Row you have to wait till row loaded into the view after committing new row edit , for that you can handle NewRowCommited event as : (see code snippet)

    
    private void NewRow_Committed(object sender, C1.WPF.DataGrid.DataGridRowEventArgs e)
            {
                Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Loaded, new Action(() =>
                 {
                     grid.Rows[e.Row.Index - 1].HeaderPresenter.Background = Brushes.Yellow;
                 }));            
            }
    
    

    Please refer the attached sample for the same : DatagridHighlight.zip

    Regards,

    Nitin

  • Posted 28 October 2021, 6:26 am EST

    Hello, Kartik,

    In my solution new rows are added at the top of the grid. In this case I get “Index out of bound”-exception when using grid.Rows[e.Row.Index - 1].

    Please extend your xaml-definition of the grid by NewRowVisibility= “Top” and you’ll see what I mean.

    Regards,

    firelex

  • Posted 28 October 2021, 8:00 am EST

    Hi,

    If you want to change RowHeader’s Background only for new row committed whether NewRowPosition is sets to “Top” or “Bottom”.

    I suggest you to handle LoadedRowHeaderPresenter for changing RowHeader’s Background. It may be possible row will add to the first, last or in the middle according to the CollectionView. You can do as: (see code snippet)

    
     public bool NewRowCommitted { get; set; }
            private void NewRow_Committed(object sender, C1.WPF.DataGrid.DataGridRowEventArgs e)
            {
                NewRowCommitted = true;
            }
            private void Grid_LoadedRowHeaderPresenter(object sender, C1.WPF.DataGrid.DataGridRowEventArgs e)
            {
                if(NewRowCommitted)
                {
                    e.Row.HeaderPresenter.Background = Brushes.Yellow;
                    NewRowCommitted = false;
                }            
            }             
    
    

    Please refer the attached sample for the same: DatagridHighlight_Mod.zip

    Regards,

    Nitin

  • Posted 3 November 2021, 3:37 am EST

    Thanks Nitin, it works!

    But what really strange is - I get LoadedRowHeaderPresenter event for every row in the grid after adding a new one. In your solution this event comes only for the new row that was just added.

    Do you have any ideas what could cause such behavour?

    Regards,

    firelex

  • Posted 3 November 2021, 4:04 am EST

    Hi,

    I’ve found it.

    As soon as I activate C1RowIndexHeaderBehavior to show rows’ indexes LoadedRowHeaderPresenter event is thrown for every row in the grid after adding a new one.

    Unfortunately your solution doesn’t work in this case. It is always the empty (first) row that is marked, not the added one.

    Regards,

    firelex

  • Posted 3 November 2021, 6:26 am EST

    Hi,

    Sorry for the inconvenience, If you are using C1RowIndexHeaderBehavior in C1DataGrid then LoadedRowHeaderPresenter will fire for all rowsheader’s presenter and sets row index on header.

    To achieve your requirement while using C1RowIndexHeaderBehavior, you can make a list of added items and update when grid’s collection changed on New Row commit. And then handle LoadedRowHeaderPresenter as : (see code snippet)

    
            public List<object> AddedRowItems = new List<object>();
            public bool IsNewRowCommit { get; set; }
            private void Grid_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
            {
                if(IsNewRowCommit)
                {
                    AddedRowItems.Add(e.NewItems[0]);
                    IsNewRowCommit = false;
                }
            }        
            private void NewRow_Committing(object sender, C1.WPF.DataGrid.DataGridEndingNewRowEventArgs e)
            {
                IsNewRowCommit = true;
            }
            private void Grid_LoadedRowHeaderPresenter(object sender, C1.WPF.DataGrid.DataGridRowEventArgs e)
            {
                if (AddedRowItems.Any(x => x == e.Row.DataItem))
                    e.Row.HeaderPresenter.Background = Brushes.Yellow;
                else
                    e.Row.HeaderPresen
    
    

    Please refer the attached modified sample for the same : DatagridHighlight_Mod2.zip

    Best Regards,

    Nitin

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels