How to edit a column in Xamarin.iOS DataGrid?
Currently Xamarin.iOS DataGrid does not directly support editing of GridCells. However, if your requirement is to edit the text of the GridCells during runtime, SfDataGrid allows you to achieve this by creating a custom GridCell derived from GridCell. You can load an editable control inside the GridCell from the sample. The edited values can be committed in the EditingChanged event handler of the UITextField class.
Refer the following code example to edit the text of GridCell in runtime.
ViewController.cs
public partial class ViewController : UIViewController { public ViewController() { } SfDataGrid grid = new SfDataGrid(); ViewModel viewmodel = new ViewModel(); public override void ViewDidLoad() { base.ViewDidLoad(); this.View.BackgroundColor = UIColor.White; grid.ItemsSource = viewmodel.Info; //Creation of a template column var textcolumn1 = new GridTextColumn { MappingName = "Name", UserCellType = typeof(CustomCell), //Adding a custom cell into the template column HeaderText = "Name" }; grid.Columns.Add(textcolumn1); View.AddSubview(grid); } public override void ViewDidLayoutSubviews() { this.grid.Frame = new CGRect(0, 20, View.Frame.Width, View.Frame.Height - 20); base.ViewDidLayoutSubviews(); } } |
CustomCell.cs
public class CustomCell:GridCell { private UITextField edit; public CustomCell () { edit = new UITextField (); edit.AllowsEditingTextAttributes = true; edit.EditingChanged += Edit_EditingChanged; this.edit.ShouldReturn += (textField) => { edit.ResignFirstResponder(); return true; }; this.AddSubview(edit); } public override void WillMoveToWindow(UIWindow window) { List<UIGestureRecognizer> gesturelist; if (this.GestureRecognizers != null) { gesturelist = new List<UIGestureRecognizer>(); foreach (var gesture in this.GestureRecognizers) gesturelist.Add(gesture); gesturelist.FirstOrDefault(ges=> ges is UITapGestureRecognizer).ShouldRecognizeSimultaneously = (gestureRecognizer, otherGestureRecognizer) => true; } base.WillMoveToWindow(window); } public void Edit_EditingChanged (object sender, EventArgs e) { var edited = this.DataColumn.Renderer.DataGrid.View.GetItemProperties (); edited.SetValue (this.DataColumn.RowData, edit.Text, this.DataColumn.GridColumn.MappingName ); } public override void LayoutSubviews () { edit.Frame = new CGRect(0,0,this.Frame.Width,this.Frame.Height); edit.Text = Convert.ToString(DataColumn.CellValue); base.LayoutSubviews (); } protected override void UnLoad() { this.RemoveFromSuperview(); } } |
The following screenshot shows the final outcome upon execution of the above code.
The working sample for this KB is available in the following location
http://www.syncfusion.com/downloads/support/directtrac/general/ze/EditText_(2)-332228580
Conclusion
I hope you enjoyed learning about how edit a column in Xamarin.iOS DataGrid.
You can refer to our Xamarin.iOS
DataGrid feature
tour page to know about its other groundbreaking feature
representations and documentation, and
how to quickly get started for configuration specifications. You can also
explore our Xamarin.iOS
DataGrid example
to
understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!