Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to make GridControl immediately save changes in a cell after editing

$
0
0

This example shows how to update GridControl's data source right after a cell editor value has been changed. To implement this scenario, we created a custom helper class exposing the CommitHelper.CommitOnValueChanged attached property.

This example uses the same idea as the one demonstrated in the E1801 - How to post values to the DXGrid DataSource immediately after changing a cell value thread. If you don't want to use custom helpers, check the solution from the E1801 example instead.

Question Comments

Added By: (no info) at: 9/6/2012 12:10:03 PM    

Great example.
One thing to note --> It may be necessary to call view.ActiveEditor.EditValue = view.Grid.GetCellValue(e.RowHandle, e.Column); after the call to view.PostEditor(); to display the value that was actually stored. The problem here is, the value that actually gets stored doesn't get displayed to the user. The call to GetCellValue will show the user the value that was actually persisted.

Added By: Gregory Colton at: 9/25/2013 9:16:28 AM    

I am getting a error stating that CommitOnValueChanged was not found in CommitHelper. Event the Example has the same error. The CommitHelper class does not have a method called CommitOnValueChanged either. Am I missing something? As it sits the example won't even compile.

Added By: Puneet Lulla at: 3/10/2014 11:37:35 PM    

Even i am getting the same problem like Gregory Colton. Please provide some assistance as soon as possible

Added By: Jaap Vliet at: 9/24/2014 2:11:00 AM    

Works only in a Tableview, but easy to edit for a Treelistview

Added By: Alexander S (DevExpress Support) at: 9/24/2014 3:57:35 AM    Hi Jaap,
Since TreeListView.CellValueChanging and GridViewBase.CellValueChanging are different events, the suggested helper is not universal. And yes, it can be easily adopted to work with TreeListView. The following code is more universal:
[C#]
classCommitHelper{publicstaticreadonlyDependencyPropertyCommitOnValueChangedProperty=DependencyProperty.RegisterAttached("CommitOnValueChanged",typeof(bool),typeof(CommitHelper),newPropertyMetadata(CommitOnValueChangedPropertyChanged));publicstaticvoidSetCommitOnValueChanged(GridColumnBaseelement,boolvalue){element.SetValue(CommitOnValueChangedProperty,value);}publicstaticboolGetCommitOnValueChanged(GridColumnBaseelement){return(bool)element.GetValue(CommitOnValueChangedProperty);}privatestaticvoidCommitOnValueChangedPropertyChanged(DependencyObjectsource,DependencyPropertyChangedEventArgse){GridColumnBasecol=sourceasGridColumnBase;if(col.View==null)Dispatcher.CurrentDispatcher.BeginInvoke(newAction<GridColumnBase,bool>((column,subscribe)=>{ToggleCellValueChanging(column,subscribe);}),col,(bool)e.NewValue);elseToggleCellValueChanging(col,(bool)e.NewValue);}privatestaticvoidToggleCellValueChanging(GridColumnBasecol,boolsubscribe){if(!(col.ViewisDataViewBase))return;if(subscribe){if(col.ViewisTreeListView)(col.ViewasTreeListView).CellValueChanging+=TreeCellValueChanging;else(col.ViewasGridViewBase).CellValueChanging+=GridCellValueChanging;}else{if(col.ViewisTreeListView)(col.ViewasTreeListView).CellValueChanging-=TreeCellValueChanging;else(col.ViewasGridViewBase).CellValueChanging-=GridCellValueChanging;}}staticvoidTreeCellValueChanging(objectsender,TreeListCellValueChangedEventArgse){if((bool)e.Column.GetValue(CommitOnValueChangedProperty))(senderasDataViewBase).PostEditor();}staticvoidGridCellValueChanging(objectsender,CellValueChangedEventArgse){if((bool)e.Column.GetValue(CommitOnValueChangedProperty))(senderasDataViewBase).PostEditor();}}
Added By: Arjan Douwes 1 at: 1/13/2015 3:53:16 AM    

This sample has a serious perfromance issue when there are many columns in a grid.
The current implementation causes the event to be called as many times as there are columns in the grid.
To resolve this issue add e.Handled = true to the TreeCellValueChanging and GridCellValueChanging eventhandlers.

The original code:
if ((bool)e.Column.GetValue(CommitOnValueChangedProperty))
               (sender as DataViewBase).PostEditor();

becomes:
if ((bool)e.Column.GetValue(CommitOnValueChangedProperty))
{
    (sender as DataViewBase).PostEditor();
    e.Handled = true;
}

Added By: Alexander Rus (DevExpress Support) at: 1/13/2015 5:09:53 AM    Hi Arjan,
Thank you for your suggestion. We have updated our example according to it.

Thanks,
Alexander

Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>