We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Updating a grid's data from a custom GridCell

I have a datagrid that I am trying to use for editing. Certain columns of the grid contain a custom cell that includes a Checkbox (could be 2 or 3 state depending on the column.)

I'm trying to find the correct way to update the data for my grid when I tap the checkbox.  The checkbox tap action is set up in the Syncfusion.SfDataGrid.GridCell.LayoutSubviews() method so it has access to the current state of DataColumn. If I try to set DataColumn.CellValue directly, it initially appears to save the value, but if I scroll away and the cell is reloaded it resets to the initial value.

It doesn't appear that much more access to the grid or its datasource outside of that. 

What is the best way to setup a custom cell so that it can update the datasource and notify the grid that there was a change?

For the record, my datasource is a Dictionary<string, object> wrapped by a helper class similar to this: 
public class DynamicModel : INotifyPropertyChanged
       public Dictionary<string, object> Values;

3 Replies

SS Sivaraman Sivagurunathan Syncfusion Team June 1, 2017 03:33 AM UTC

Hi Clint Anderson, 
Thanks for contacting Syncfusion Support. 
We have checked your query and prepared sample based on your requirement. if your using current version of Syncfusion controls you can use GridSwitchColumn to achieve your requirement or else use UserCellType property to load the UISwitch in the in GridTextColumn. In that you need to override the OnCellValueChanged method to update the cell value. 
We have prepared sample base on your requirement and attach sample for your reference. You can download the same from the below link. 

CA Clint Anderson June 1, 2017 01:47 PM UTC

When trying to run the UserCellType sample, I get an error when it first launches.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
  at System.ThrowHelper.ThrowKeyNotFoundException () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/
  at System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) [0x00021] in /Library/Frameworks/Xamarin.iOS.framework/Versions/
  at at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /Library/Frameworks/Xamarin.iOS.framework/Versions/
  --- End of inner exception stack trace ---
  at Syncfusion.Data.Extensions.PropertyDescriptorExtensions.GetComplexPropertyValue (System.String[] propertyNameList, System.ComponentModel.PropertyDescriptorCollection tempitempproperties, System.Object record) [0x000c4] in <e17fdc80dec148f9aa934771c4c9744c>:0
  at Syncfusion.Data.ItemPropertiesProvider.GetValue (System.Object record, System.String property) [0x0005d] in <e17fdc80dec148f9aa934771c4c9744c>:0
  at Syncfusion.SfDataGrid.SfDataGridHelpers.GetCellValue (Syncfusion.SfDataGrid.SfDataGrid datagrid, System.Object recorddata, System.String columnname) [0x0001b] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.GridCellTextViewRenderer.OnUpdateCellValue (Syncfusion.SfDataGrid.DataColumnBase dataColumn) [0x00018] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.GridCellRendererBase.UpdateCellValue (Syncfusion.SfDataGrid.DataColumnBase dataColumn) [0x00000] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.DataColumn.OnInitializeColumnElement (System.Object rowData, System.Boolean isInEdit) [0x00017] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.DataColumnBase.InitializeColumnElement (System.Object rowData, System.Boolean isInEdit) [0x00000] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.DataRow.CreateColumn (System.Int32 index, System.Int32 columnHeightIncrementation) [0x00197] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.DataRow.OnGenerateVisibleColumns (Syncfusion.GridCommon.ScrollAxis.VisibleLinesCollection visibleColumnLines) [0x00080] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.DataRowBase.InitializeDataRow (Syncfusion.GridCommon.ScrollAxis.VisibleLinesCollection visibleColumnsLines) [0x0000c] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.RowGenerator.CreateDataRow (System.Int32 rowIndex, Syncfusion.GridCommon.ScrollAxis.VisibleLinesCollection visibleColumns, Syncfusion.SfDataGrid.RowRegion region) [0x00091] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.RowGenerator.PregenerateRows () [0x000a8] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.VisualContainer.pregenerateItems () [0x00000] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.VisualContainer.OnMeasure () [0x00170] in <01ce1d4a7ce445629578c8d306927060>:0
  at Syncfusion.SfDataGrid.VisualContainer.LayoutSubviews () [0x0004f] in <01ce1d4a7ce445629578c8d306927060>:0
  at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/4466/a04678c2/source/xamarin-macios/src/UIKit/UIApplication.cs:79
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/4466/a04678c2/source/xamarin-macios/src/UIKit/UIApplication.cs:63
  at customGridCell_Dictionary.Application.Main (System.String[] args) [0x00008] in /Users/canderson/Downloads/customGridCell_Dictionary/customGridCell_Dictionary/Main.cs:12

SS Sivaraman Sivagurunathan Syncfusion Team June 5, 2017 03:30 AM UTC

Hi Clint Anderson, 
Thanks for your patience. 
We have checked your query. In our side we were unable to reproduce your reported issue. The sample was working fine in our side. We have attached sample and sample runnable video for your reference from below link 
Incase if you are able to reproduce the issue from your side, please revert us with the modified sample. 

Live Chat Icon For mobile
Up arrow icon