开发者

Silverlight 4 Add New Item To DomainDataSource

I have a simple datagrid listing addresses and a child window where the user can edit/add new. On the main form with the datagrid i have a button to "Insert New Address" which should load 开发者_开发技巧the child window with an empty Address object. However it will not let me add a record. Am i doing something wrong? my current code is as follows:

    Dim address As New Address
    Dim frmAddressObj As New frmAddress

    If frmAddressObj.AddressDomainDataSource.DataView.CanAdd = False Then
        frmAddressObj.AddressDomainDataSource.Load()
    End If
    frmAddressObj.AddressDomainDataSource.DataView.Add(address)

Address is the address object. frmAddress is the child window form. AddressDomainDataSource is the same datasource i use in the datagrid as i use in the child. CanAdd is always false and i got told to try loading before adding but this does not appear to have helped. When it reaches the Add method it returns an Exception of 'Add' is not supported by this ICollectionView. Any help would be appreciated. Thanks


The DataView field should be thought of as a read-only collection. The simplest general usage of the DomainDataSource with a DataGrid goes something like this:

(myDataSource.DomainContext as myDomainContext).my_entitys.Remove(dgOrders.SelectedItem as order);
(myDataSource.DomainContext as myDomainContext).SubmitChanges();

It is similar for insert, you just use

my_entitys.Add(myNewEntityInstance); 

instead of

my_entitys.Remove(entityToRemove);

And for updates you simply call

(myDataSource.DomainContext as myDomainContext).SubmitChanges();


You must also have the insert method in your domain service. So make sure you have a method that looks like:

Public Sub InsertAddress(address As Address)
End Sub

or in C#

public void InsertAddress(Address address)


I had the same problem recently and in my case, the DomainDataSource wasn't loaded (or even bound to its context yet) because it resided in a TabItem that wasn't selected.

Make sure you have your DomainDataSource properly loaded in the visual tree, that solved the issue in my case.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜