0xbadf00d
0xbadf00d

Reputation: 18218

SL4 | DomainDataSource / DataGrid | Adding a blank row to insert new items

I'm trying to bind a DataForm and DataGrid to a DomainDataSource and implement the functionality of adding, deleting and editing items.

Everything works very well for the DataForm part. But how I'm able to add a new record using the DataGrid? Like I know until now, there are two working options:

  1. Add a new - "blank" - item to the DataView.

    • Problem: I'm getting validatoin errors as a result of key requirements and constraints.
  2. Using the "SDK feature to enable Add New Row capabilities in DataGrid control" from Silverlight 4 service release (September 2010)

    • Problem: What exactly has changed? There are no new members or a documentation.

Here are some basic markup declarations matching the most important parts of my project:

<Grid x:Name="LayoutRoot">
    <sdk:DataGrid x:Name="ParentGrid" AutoGenerateColumns="False" ItemsSource="{Binding ElementName=parentDomainDataSource, Path=Data}"/>
    <toolkit:DataForm x:Name="ParentForm" CommandButtonsVisibility="All" Grid.Row="1" ItemsSource="{Binding ElementName=parentDomainDataSource, Path=Data}"/>

    <sdk:DataGrid x:Name="ChildGrid" Grid.Column="1" AutoGenerateColumns="False" ItemsSource="{Binding ElementName=childDomainDataSource, Path=Data}"/>
    <toolkit:DataForm x:Name="ChildForm" CommandButtonsVisibility="All" ItemsSource="{Binding ElementName=childDomainDataSource, Path=Data}"/>

    <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my1:Child, CreateList=true}" Name="childDomainDataSource" QueryName="GetChildrenQuery"
                                  DomainContext="{StaticResource domainCtx}"/>
    </riaControls:DomainDataSource>
    <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my1:Parent, CreateList=true}" Name="parentDomainDataSource" QueryName="GetParentsQuery" 
                                  DomainContext="{StaticResource domainCtx}"/>
</Grid>

Unfortunately, I'm running out of time.

Thanks in advance for any help. Best regards from Germany. Hope anybody can help ;)

Upvotes: 1

Views: 3511

Answers (2)

Chui Tey
Chui Tey

Reputation: 5584

Although it doesn't strictly answer your question, using a PagedCollectionView works rather nicely. In addition, you can hit Esc to cancel the row being inserted.

The downside is you have to do a little bit of book-keeping.

private DomainService1 ctx = new DomainService1();
private PagedCollectionView pcvPersons = null;
private List<Person> tmpList = null;

private void LoadData()
{
    ctx.Load(ctx.GetPersonsQuery(),
        (op) =>
        {
            tmpList = new List<Person>(ctx.Persons);
            pcvPersons = new PagedCollectionView(tmpList);
            dataGrid1.ItemsSource = pcvPersons;
        }, null);
}

private void AddButton_Click(object sender, RoutedEventArgs e)
{
    Web.Person newItem = pcvPersons.AddNew();
}

Upvotes: 0

Maverik
Maverik

Reputation: 5681

I just ran into this question trying to figure out similar thing. Posting here in the hope that this saves somebody else the time:

In the code-behind for a "New Record" Button's Click:

private void OnNewRecordClick(object sender, System.Windows.RoutedEventArgs e)
{ childDomainDataSource.DataView.Add(new Child()); }

The DataGrid picks up the new record instantly and you can still do SubmitChangesCommand on DDS to post the new entry back to DB.

Upvotes: 0

Related Questions