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.
Unfortunately, activation email could not send to your email. Please try again.

How to edit a column in SfDataGrid?

Currently SfDataGrid 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

 

 

 

 

 

 

 

Article ID: Published Date: Last Revised Date: Platform: Control:
6206 02/03/2016 12/21/2016 Xamarin.iOS SfDataGrid
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.