This is a migrated thread and some comments may be shown as answers.

Problem with AddingNewDataItem and DomainDataSource

4 Answers 105 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Serhiy Volhushyn
Top achievements
Rank 1
Serhiy Volhushyn asked on 03 Mar 2011, 06:06 PM

I'm using the latest build 2010.3.1501 (tested with SP1 too).
I use RadGridView with DomainDataSource and looks like there a bug when inserting rows.
If I subscribe to AddingNewDataItem and return new entity,
In RowEditEnded event:
* e.EditOperationType is always "Edit" instead of "Insert"
* DomainDataSource.HasChanges is always "False" instead of "True"
* and, of course. DomainDataSource.SubmitChanges() does not submit anything.

Here's the source code to reproduce:

<UserControl
    x:Class="Accellos.DataServices.WUI.FileMaintenance.Controls.Test1.MyWorkspaceControl1"
    xmlns:mt="clr-namespace:Accellos.DataServices.MT.Client.DotNet;assembly=Accellos.DataServices.MT.Client.AccellosOneServiceAsync.SL"
    xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices"
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
    >
  
    <Grid>
        <riaControls:DomainDataSource QueryName="GetAccounts" AutoLoad="True" x:Name="dds" DataContext="{Binding}">
            <riaControls:DomainDataSource.DomainContext>
                <mt:AccountDomainContext/>
            </riaControls:DomainDataSource.DomainContext>
  
            <riaControls:DomainDataSource.SortDescriptors>
                <riaControls:SortDescriptor PropertyPath="Id" />
            </riaControls:DomainDataSource.SortDescriptors>
        </riaControls:DomainDataSource>
  
        <telerik:RadGridView x:Name="gridView1" AutoGenerateColumns="True" ItemsSource="{Binding Data, ElementName=dds}"/>
    </Grid>
</UserControl>

using System;
using Accellos.DataServices.MT.Client.DotNet;
using Accellos.DataServices.MT.Common.DTO;
using Accellos.Platform.Silverlight.Workspace;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
  
namespace Accellos.DataServices.WUI.FileMaintenance.Controls.Test1
{
    public partial class MyWorkspaceControl1
    {
        #region Constructor
        public MyWorkspaceControl1(IWorkspace workspace)
        {
            InitializeComponent();
  
            if (workspace == null)
                throw new ArgumentNullException("workspace");
  
            DataServicesApplication application = (DataServicesApplication)workspace.WorkspaceContainer.GetApplicationById(DataServicesApplication.ApplicationId);
            ((AccountDomainClient)dds.DomainContext.DomainClient).MiddleTierClient = application.CreateMiddleTierServiceClient();
  
            gridView1.AddingNewDataItem +=
                delegate(object sender, GridViewAddingNewEventArgs e)
                    {
                        e.NewObject = new Account();
                    };
  
            gridView1.RowEditEnded +=
                delegate(object o, GridViewRowEditEndedEventArgs e)
                {
                    if (e.EditAction != GridViewEditAction.Commit)
                    {
                        if (!dds.IsSubmittingChanges)
                            dds.RejectChanges();
  
                        return;
                    }
  
                    // Is "Edit" instead of "Insert" when inserting
                    GridViewEditOperationType operation = e.EditOperationType;
                    // Always "False" when inserting
                    bool hasChanges = dds.HasChanges;
  
                    // When inserting does not submit any changes
                    if (!dds.IsSubmittingChanges)
                        dds.SubmitChanges();
                };
        }
        #endregion
    }
}

If I comment AddingNewDataItem everything works as expected.

Does anyone knows if there any workaround ?

4 Answers, 1 is accepted

Sort by
0
Accepted
Nedyalko Nikolov
Telerik team
answered on 08 Mar 2011, 04:59 PM
Hi Serhiy Volhushyn,


Indeed you are right about the behavior of RadGridView control. Due to the fact that IEditableCollectionView doesn't have an overload of the AddNew() method which accepts custom object RadGridView actually adds item to the collection and put this item into edit mode (which already is an edit operation). However you could workaround this by using RadGridView insert row UI (RadGridView.ShowInsertRow = true).
We are aware about the problem with RadGridView + DomainDataSource and adding new items (HasChanges is always false). There are two possible ways to solve it:

1. Use RadDomainDataSource instead of DomainDataSource - here you could find a little bit more information about CRUD operations with RadDomainDataSource.
2. You could just add the edited item to DomainDataSource collection:

Copy Code

// just attach the edited item to DomainDataSource

this.rdds.DataView.Add(e.EditedItem);


I'm attaching my sample application for a reference.


All the best,
Nedyalko Nikolov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Serhiy Volhushyn
Top achievements
Rank 1
answered on 08 Mar 2011, 10:45 PM
Thank you for the hint.
When I use RadDomainDataSource it fixes dds.HasChanges problem and creates new record in database when dds.SubmitChanges()

But e.EditOperationType is still "Edit" instead of "Insert".
0
Accepted
Nedyalko Nikolov
Telerik team
answered on 09 Mar 2011, 02:19 PM
Hello Serhiy Volhushyn,

We are aware about this behavior, but there is no other way to add item different from the default one (created by parameterless constructor). RadGridView uses IEditableCollectionView interface to deal with editing (adding) operations.
Due to the fact that IEditableCollectionView doesn't have an overload of the AddNew() method which accepts custom object RadGridView actually adds item to the collection and put this item into edit mode (which already is an edit operation). However you could work around this by using RadGridView insert row UI (RadGridView.ShowInsertRow = true).

Regards,
Nedyalko Nikolov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Serhiy Volhushyn
Top achievements
Rank 1
answered on 09 Mar 2011, 04:15 PM
Thank you for the reply.
I cannot use insert row UI, but I found a workaround:

private GridViewEditOperationType _editOperationType = GridViewEditOperationType.Edit;
  
  
private void OnEntityGridViewBeginningEdit(object sender, GridViewBeginningEditRoutedEventArgs e)
{
    _editOperationType = GridViewEditOperationType.Edit;
}
  
private void OnEntityGridViewAddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
{
    e.NewObject = CreateEmptyEntity();
  
    _editOperationType = GridViewEditOperationType.Insert;
}
Tags
GridView
Asked by
Serhiy Volhushyn
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Serhiy Volhushyn
Top achievements
Rank 1
Share this question
or