Using the DataForm control
Add A DataForm Control and bind it to the current item of the DataGrid. Open Views\HomePage.xaml, and add a DataForm Control right after the DataPager.<Grid x:Name="LayoutRoot" Background="White">
...
<dataControls:DataPager PageSize="3"
Source="{Binding Data, ElementName=customersDataSource}" />
<dataControls:DataForm x:Name="customerDataForm"
Header="Customer Data"
CurrentItem="{Binding ElementName=dataGrid, Path=SelectedItem}">
</dataControls:DataForm>
...
</Grid>
</Grid>

To edit an item, click the pencil icon on the DataForm.

When you do that, the controls will be replaced with input controls, and Save and Cancel buttons will show up.

If you change a value of a property in a customer’s data, you’ll get a notification that this data was changed and the customer is not “dirty”.

Notice that changes that are made here are tracked only in memory. This means, that by pressing the Save button, you’ll switch over to Display more, but the data is not saved to the server. To enable submitting changes back to the server, we need to add a submit button below the DataForm.
<Button x:Name="btnSubmit"
Width="120"
Click="btnSubmit_Click"
Content="Submit" />
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
this.customerDataForm.CommitItemEdit();
this.customersDataSource.SubmitChanges();
}
Adding Validation Logic using Data Annotations
One of the great things in .Net RIA Services is that code that we wrote in the server side can be resued in the client side. This means that we can reuse the validation annotations that we have for our domain entities.To annotate entities with validation semantics, open the metadata file for the domain service (BankDomainService.metadata.cs in the sample). In this file, you’ll find partial classes that match your domain classes (class Customer in this sample), decorated with MetadataTypeAttribute pointing to a metadata type the is also generated here as an inner class. The metadata class has fields / properties that match the properties that the original class has, that you can annotate with validation requirements.
[MetadataTypeAttribute(typeof(Customer.CustomerMetadata))]
public partial class Customer
{
internal sealed class CustomerMetadata
{
public int CustomerID;
public bool IsBusiness;
public string Name;
public string City;
public string Email;
public EntitySet<Account> Accounts;
}
}
internal sealed class CustomerMetadata
{
public int CustomerID;
public bool IsBusiness;
[Required(ErrorMessage="Please provide a name for the customer")]
[StringLength(30,
ErrorMessage="Customer's name cannot be more than 30 characters")]
public string Name;
[StringLength(30,
ErrorMessage = "Customer's city cannot be more than 15 characters")]
public string City;
[RegularExpression(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$",
ErrorMessage="Please provide a valid email address")]
public string Email;
public EntitySet<Account> Accounts;
}
If you now run the application, and try to update a customer’s entity with values that do not match the validations, you will get a visual notification and the data will not be updated to the server.

In this post I talked about the new DataForm control in .Net RIA Services, showed how to add it to our application and how it works. Then we talked a little bit about annotating the entities with validation requirements and saw how the DataForm enforces them.
Enjoy!



0 Comments