开发者

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

开发者 https://www.devze.com 2023-01-31 19:18 出处:网络
I\'m trying to bind a DataForm and DataGrid to a DomainDataSource and implement the functionality of adding, deleting and editing 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 ;)


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.


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消