Telerik blogs

I will show you how easy is to integrate OpenAccess with RadGridView for Silverlight. I get the example from my colleague Dimitur and make it work with our RadGridView. If you are interested how to make OpenAccess to work with ADO.NET Data Services please read his blog series.

To allow inserting new rows in RadGridView we should attach to AddingNewDataItem event.

<telerik:RadGridView x:Name="gridView" Grid.Row="1" AddingNewDataItem="GridViewAddingNewDataItem" />

And provide the newly created object in it.

private void GridViewAddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
{
 e.NewObject = new Supplier();
}

Now we can insert new objects.

Next steps is to save changes to the database. We should attach to GridViewRow.EditEndedEvent.

this.gridView.AddHandler(GridViewRow.EditEndedEvent, new EventHandler<RecordRoutedEventArgs>(this.OnGridViewRowEditEnded));

This event occurs when the editing of the row ends. And this is our handler:

private void OnGridViewRowEditEnded(object sender, RecordRoutedEventArgs e)
{
    DataRecord record = (DataRecord)e.Record;
    Supplier supplier = (Supplier)record.Data;

    // if the record is disconnected we add the new item to the collection that we use as data source
    if (record.IsDisconnected)
    {
        this.suppliers.Add(supplier);
        this.context.AddToSuppliers(supplier);
        this.context.BeginSaveChanges(SaveChangesOptions.ContinueOnError, this.OnSaveAllComplete, null);
        this.gridView.Rebind();
    }
    else
    {
        this.context.UpdateObject(supplier);
        this.context.BeginSaveChanges(SaveChangesOptions.ContinueOnError, this.OnSaveAllComplete, null);
    }
}

We can have two states of our records. If it is disconnected this mean that the record is new and we should add it to the database. If it isn't we should update it to the database.

GridViewOpenAccess

In this demo you can find also how to delete the current selected item.

Enjoy!


Related Posts

Comments

Comments are disabled in preview mode.