Quantcast
Channel: Telerik Blogs
Viewing all articles
Browse latest Browse all 5210

RadDataGrid for Windows 8 XAML enhancements – Inline editing and data validation

$
0
0

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:

publicclassTaskData : ValidateViewModelBase
  {
     publicDateTime? StartDate
     {
            get{ returnstartDate; }
            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:

publicclassCustomCommitEditCommand : ICommand
    {
        publicboolCanExecute(objectparameter)
        {
            returntrue;
        }
 
        publiceventEventHandler CanExecuteChanged;
 
        publicvoidExecute(objectparameter)
        {
            this.SubmitChanges();
 
        }
 
        privateasync voidSubmitChanges()
        {
            MessageDialog md = newMessageDialog("Changes were saved successfully.", "Data Saved.");
            bool? result = null;
            md.Commands.Add(
               newUICommand("OK", newUICommandInvokedHandler((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






Viewing all articles
Browse latest Browse all 5210

Trending Articles