MAUI | ComponentOne
Controls / FlexGrid / Editing
In This Topic
    Editing
    In This Topic

    FlexGrid has built-in support for fast, Excel like in-cell editing. There is no need to add extra columns with Edit buttons that switch between display and edit modes. Following are the ways to perform data editing in the grid.

    Custom Editing

    FlexGrid allows you to add various methods while performing editing in the cells. Using these methods, you can customize the overall editing experience. You can add a confirmation message box that pops up to confirm whether a user is sure or not about committing a particular change, providing an attractive and flexible way to update data.

    In addition, FlexGrid fires various events to facilitate better control on the editing process. The following table lists the events which are fired by the grid during the editing process.

    Event Name Description
    BeginningEdit The event occurs when the editing process in a cell is about to start.
    CellEditEnded The event occurs after the editing process of a cell is committed.
    CellEditEnding The event occurs before the editing process in a cell is committed.
    PrepareCellForEdit The event occurs before an editor cell becomes active and it is created.

    The following image shows how the FlexGrid appears with an Edit confirmation message box.

    The following example demonstrates how to implement custom editing in FlexGrid to display a confirmation pop-up when a user wants to commit a change. In this example, FlexGrid fires BeginningEdit and CellEditEnded events to customize the editing process and display the confirmation alert dialog. This example uses the sample created in the Quick Start topic.

    1. In MainPage.xaml, edit the code to assign the OnBeginningEdit method as its BeginningEdit event handler and the OnCellEditEnded method as its CellEditEnded event handler as shown in the following code.
      XAML
      Copy Code
      <c1:FlexGrid x:Name="grid" AutoGenerateColumns="False" BeginningEdit="OnBeginningEdit" CellEditEnded="OnCellEditEnded">
          <c1:FlexGrid.Columns>
              <c1:GridColumn Binding="FirstName" />
              <c1:GridColumn Binding="LastName" />
              <c1:GridColumn Binding="City"/>
              <c1:GridColumn Binding="Country"/>
              <c1:GridColumn Binding="Active"/>
          </c1:FlexGrid.Columns>
      </c1:FlexGrid>
      
    2. Implement the OnBeginningEdit and OnCellEditEnded handler methods to perform custom editing and display a confirmation alert dialog.
      C#
      Copy Code
      private object _originalValue;
      
      private void OnBeginningEdit(object sender, GridCellEditEventArgs e)
      {
          _originalValue = grid[e.CellRange.Row, e.CellRange.Column];
      }
      
      private void OnCellEditEnded(object sender, GridCellEditEventArgs e)
      {
          var originalValue = _originalValue;
          var currentValue = grid[e.CellRange.Row, e.CellRange.Column];
          if (!e.CancelEdits && (originalValue == null && currentValue != null || !originalValue.Equals(currentValue)))
          {
              DisplayAlert("Confirmation", "Do you want to commit the Edit?", "Apply", "Cancel").ContinueWith(t =>
              {
                  if (!t.Result)
                  {
                      grid[e.CellRange.Row, e.CellRange.Column] = originalValue;
                  }
              }, TaskScheduler.FromCurrentSynchronizationContext());
          }
      }