Telerik blogs

Overview

Since Q2 we have introduced new vital feature for our RadDataGrid – editing. It includes build-in strongly typed editors as well as data validation support. So, using the component for editing scenarios is as simple as possible. Just set UserEditMode and provide the appropriate ItemsSource and you are done.

<telerikGrid:RadDataGrid
               DataContext="{StaticResource Model}"
               ItemsSource="{Binding Data}”
               UserEditMode="Inline"/>



Validation

Of course when we talk about editing, we need some mechanism to validate our data. With our DataGrid we have provided easy to use API through the INotifyDataErrorInfo interface and our ValidateViewModelBase class that implements it. So, if a certain property needs certain validation rules it can do with a few lines of code:

public class TaskData : ValidateViewModelBase
  {
     public DateTime? StartDate
     {
            get { return startDate; }
            set
            {
                startDate = value;
 
                if (startDate > endDate)
                {
                    this.AddError("StartDate", "Start Date cannot be set after the end date.");
                }
                else
                {
                    this.RemoveErrors("StartDate");
                }
 
                this.OnPropertyChanged();
            }
        }
   }

Doing that will provide RadDataGrid with enough information to trigger validation during editing.


 

Commands

The other interesting face of the editing is the flexible MVVM friendly API. RadDataGrid exposes several commands that cover the whole edit operation:

  • BeginEdit command
  • CommitEdit command
  • CancelEdit command
  • ValidateCell command

We leveraged the current command infrastructure available in RadDataGrid from the previous versions. Here is an example how to utilize CommitEditCommand:

public class CustomCommitEditCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }
 
        public event EventHandler CanExecuteChanged;
 
        public void Execute(object parameter)
        {
            this.SubmitChanges();
 
        }
 
        private async void SubmitChanges()
        {
            MessageDialog md = new MessageDialog("Changes were saved successfully.", "Data Saved.");
            bool? result = null;
            md.Commands.Add(
               new UICommand("OK", new UICommandInvokedHandler((cmd) => result = true)));
 
            await md.ShowAsync();
        }
    }


<telerikGrid:RadDataGrid.Commands>
      <telerikGridCommands:DataGridUserCommand
Command="{Binding CommitEditCommand, Source={StaticResource Model}}"
Id="CommitEdit" 
EnableDefaultCommand="True"/>
</telerikGrid:RadDataGrid.Commands>


All these and more are available for download in our RadControls for Windows 8 suite.

Download Source






About the Author

Tsvyatko Konov

Tsvyatko Konov was the Lead of UI for Xamarin and UI for UWP team.

Comments

Comments are disabled in preview mode.