AddingNewDataItem and HasChanges

14 posts, 0 answers
  1. Louis Bouchard
    Louis Bouchard avatar
    94 posts
    Member since:
    Feb 2010

    Posted 13 Jul 2010 Link to this post

    Hello,

    I use the e.NewObject to put some values in some fields.  Everything works fine and I see my values in the new row.

    But when the event RowEditEnded is called, the HasChanges of the DomainContext is always false (it's not the case when I update a existing row).  So, the insert is never done.  Do you know how I can "force" the HasChanges to true?

    I try to Load the RadGridView in AddingNewDataItem but nothing work.
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 14 Jul 2010 Link to this post

    Hi Louis Bouchard,

    There are a couple of options to resolve the issue. Firstly, you may use the BeginningEdit event to insert the new item into the grid. Another possible approach is to add manually the newly created object into your DomainContext during the AddingNewDataItem event. For example:

    (this.customers.DomainContext as CustomersDomainContext).Customers.Add(newCustomer);

    Where customers is the name of the DomainDataSource and newCustomer is the inserted item.
    In both cases you need to submit the changes into the DomainDataSource:
    this.customers.SubmitChanges();



    Sincerely yours,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012

    Posted 26 Apr 2011 Link to this post

    Hello Maya,
    I was having the same issue and I was wondering why this is not done automaticaly by the grid?
    Why do I need to call this:
    this.customers.DomainContext as CustomersDomainContext).Customers.Add(newCustomer);

    I'm asking this because when I don't set the e.NewObject in AddingNewDataItem,
    the domain context seems to be notified by the changes...
    but then I can't set the initial values of my object...

    Is there something I do wrong?

    My project uses RIA Services. Nothing fancy. All I want to do is set initial values in the AddingNewDataItem event.

    Thank you for your help,
    John.


  5. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 27 Apr 2011 Link to this post

    Hello John,

    I have tested the scenario with inserting a new item in AddingNewDataItem event and submitting the changes during RowEditEnded event:

    void customersGrid_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
            {
                this.customers.DomainContext.SubmitChanges();
            }
     
            private void customersGrid_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
            {
                e.NewObject = new Customer() { CustomerID = "AMAT" };
            }

    and with the current version everything seems to work correct. Still, I am sending you the sample project I used for testing the case. May you take a look at it and let me know in case of any misunderstandings ?
     


    Greetings,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012

    Posted 27 Apr 2011 Link to this post

    Hello Maya,
    Thank you for your reply.

    You cannot see the problem because you SubmitChanges in RowEditEnded. That is not what I want.
    I want to be able to add several rows before submiting my changes.

    My test case is sligltly different in the way that the content of my grid is filtered by a combobox. I have to find some time to build a proper sample.

    But, please, stay with me as I have two other related problems to report ;)

    To reproduce my first problem:

    - EMPTY the customers table (very important), empty all the tables to be sure.

    - set showinsert to true
    - and add a save button
    - change query name to QueryName="GetCustomers"

    here is the xaml:
    <UserControl x:Class="RadGridView_RDDS.MainPage"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                 xmlns:local="clr-namespace:RadGridView_RDDS.Web"
                 mc:Ignorable="d"
                 d:DesignHeight="300" d:DesignWidth="400">
     
        <Grid x:Name="LayoutRoot" Background="White">
            <telerik:RadDomainDataSource x:Name="customers" AutoLoad="True" QueryName="GetCustomers">
                <telerik:RadDomainDataSource.DomainContext>
                    <local:DomainService1 />
                </telerik:RadDomainDataSource.DomainContext>
            </telerik:RadDomainDataSource>
            <StackPanel>
                <Button Content="Enregistrer" Name="btnSaveOptions" Width="132" Margin="5" IsEnabled="{Binding ElementName=customers, Path=HasChanges}"  Click="btnSaveOptions_Click" />
     
                <telerik:RadGridView x:Name="customersGrid" ItemsSource="{Binding DataView, ElementName=customers}"
                                 AddingNewDataItem="customersGrid_AddingNewDataItem"
                                 AutoGenerateColumns="True" ShowInsertRow="True">
                </telerik:RadGridView>
            </StackPanel>
        </Grid>
    </UserControl>

    Here is the code:
    namespace RadGridView_RDDS
    {
        public partial class MainPage : UserControl
        {      
            public MainPage()
            {
                InitializeComponent();
                this.customersGrid.RowEditEnded += new EventHandler<GridViewRowEditEndedEventArgs>(customersGrid_RowEditEnded);
            }
     
            void customersGrid_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
            {
                //this.customers.DomainContext.SubmitChanges();
            }
     
            private void customersGrid_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
            {
                e.NewObject = new Customer() { CustomerID = "AMAT" };
            }
     
            private void btnSaveOptions_Click(object sender, RoutedEventArgs e)
            {
                this.customers.DomainContext.SubmitChanges();
            }
        }
    }

    - run (empty tables, ok ?)
    - click on the insert row
    - click on the companyname cell
    - type test in companyname
    - press return
    - the row gets added to the grid, stays there for a second then disapears (magic)
    - press save (enregistrer), the row is displayed correctly

    This is my first problem, the row is disapearing... and only comes back after the submit changes...

    The second thing is this. With the same sample and empty tables.
    - run
    - click on insert row
    - now, press esc because you want to cancel the row you added
    - esc again, the row disapears, but the collection still has changes, you can see because the save button is enabled
    How can I cancel the row addition in that case?

    Note that I have a long history of struggling with your ShowInsertRoow feature.
    Please see the latest case I had open and make sure the corrections were integrated in the latest release...

    Thank you for your help Maya,
    John.

    ps: if you want I can send you my whole project if you can't repro my problems...


  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 28 Apr 2011 Link to this post

    Hello John,

    I have tested the first scenario you described with completely new data base with empty table, but still I was not able to reproduce it. I am attaching the sample I used for reproducing it. May you take a look at it and let me know whether you can get the same behavior on it?
    As for the second issue, it will be the expected one. You may try to call RejectChanges() in case the edit action is cancel or provide a separate variable saving the information for the cancellation of the changes.
     

    Regards,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. vk
    vk avatar
    46 posts
    Member since:
    Jul 2012

    Posted 09 May 2011 Link to this post

    Hello Maya,

    I'm having exactly the same issues with ShowInsertRow feature as John, but also I have noticed some strange behavior of the RadGridView control. You can reproduce it using the project you have attached.

    1) Add a couple of rows using the InsertNewRow area
    2) Click on the InsertNewRow again to add third row and hit escape key twice to cancel the row
    3) Enter edit mode of any row in the grid
    4) Enter edit mode of another row int the grid...

    I have added some screenshots that shows the problem.
    2.jpg has been made right after the forth step. And 3.jpg is the result of just clicking on rows to enter and leave edit mode.
  9. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012

    Posted 09 May 2011 Link to this post

    Hello Maya,
    Sorry for the delay.

    You say that I shall call the rejectchanges to cancel my changes... I understand that...

    But when I don't use AddingNewDataItem to setup my object and press ESC the changes are canceled.
    It should work the same, no ? I cannot rejectchanges because I may have created other rows before this one and I only want to cancel this one not all the rows. How can this be done ?

    As of valentine, you describe of to reproduce your problem but you should also say what your problem is because maybe Maya or I will not have the same result. Do you get an exception ? Give more details about what happens after you follows your steps... This will help a lot...

    Thank you for your help,
    John.
  10. vk
    vk avatar
    46 posts
    Member since:
    Jul 2012

    Posted 09 May 2011 Link to this post

    I've updated my post with screenshots to show unexpected gridview behavior.

    John, as for your second issue, here what I came up with:

    private object tempNewItem = null;
      
    private void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e) 
    {
         var grid = e.OwnerGridViewItemsControl;
         Declaration newDeclaration = new Declaration();
         newDeclaration.DeclarationDate = DateTime.Today;
         e.NewObject = newDeclaration;
                  
          tempNewItem = e.NewObject;
    }
      
    private void GridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
    {
         RadGridView grid = sender as RadGridView;
      
         if (e.EditAction == Telerik.Windows.Controls.GridView.GridViewEditAction.Cancel)
          {
               if (e.Row.Item == tempNewItem)
                   {
                        DeclarationsViewModel vm = this.DataContext as DeclarationsViewModel;
                      vm.DomainContext.Declarations.Remove(e.Row.Item as Declaration);
                      grid.Items.Remove(e.Row.Item);
               tempNewItem = null;

                   }
          {
    }
  11. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012

    Posted 09 May 2011 Link to this post

    Thank you Valentine,

    This will work but I would like to avoid doing this. I do not like to have a temp variable holding the item for later use.

    Also I think the grid should only add the item to the collection after roweditended.
    That is what it's  doing you use the insert new row but do not initialize your item in addinNewDataItem...
    Without addinNewDataItem, when you press ESC, they are no changes to the collection...

    But in case there is not other solution I'll do it like that. Thanks a lot for the tip...

    John.
  12. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 10 May 2011 Link to this post

    Hello,

    @John:
    If you require to handle the AddingNewDataItem event in the above mentioned manner, you may try to verify the EditAction during the RowEditEnded event - whether it is Cancel or Commit or the EditOperationType - whether whether it is Insert and call SubmitChanges() or RejectChanges() accordingly. Please do let me know whether this approach will suit your needs.
    @Valentine:
    Indeed, we have managed to reproduce the issue and we will do our best to fix it as soon as possible. Thank you for reporting it.
     

    Regards,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  13. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012

    Posted 10 May 2011 Link to this post

    Hello Maya,
    Maybe I did not get something so do not hesitate to correct me if I'm wrong...
    I am inserting or modifiying more then one line before I want to submit my changes...
    That means that I can't call RejectChanges() to cancel the current line because this will cancel all the changes I made and that are still not saved by a previous SubmitChanges()... Right ?

    How is it that by default the current row can be cancelled when I do not use AddingNewDataItem but can't be canceled when I use it?

    Thank you for your help,
    John.
  14. vk
    vk avatar
    46 posts
    Member since:
    Jul 2012

    Posted 10 May 2011 Link to this post

    It seems I've found more elegant way to cancel the inserted row without having to call RejectChanges in the RowEditEnded event. You still can use the AddingNewDataItem event.

    private void GridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
      {
          if (e.EditAction == Telerik.Windows.Controls.GridView.GridViewEditAction.Cancel
              && e.Row.GetType() == typeof(Telerik.Windows.Controls.GridView.GridViewNewRow))
          {                
                  (sender as RadGridView).Items.Remove(e.Row.Item);
          }            
      }
  15. Jonx
    Jonx avatar
    258 posts
    Member since:
    Jul 2012

    Posted 23 Jun 2011 Link to this post

    Hello Valentine,
    Your ode is definitely not working oor at least it does not handle all the cases and raizes some exceptions from time to time and even more often then that.
    I'm sat with the insertion of new rows using the InsertNewRow and AddingNewDataItem. I've struggled with it the last 6 months in all my projects and it never gave somethin very satisfying.
    I've come to the conclusion that the technical side is not yet very mature.
    You get problems here and there in the chain of code that is called.
    I've got problems for weeks with disapearing items just because of some bugs not handling the new inserted items.
    When it's not the fact that the templates are not applied for the new insert row.
    Etc. All those problems are solved for now but they are always new ones...
    And also why is it working diffrently when using AddingNewDataItem and when not using it.
    When you are not using it, you just press cancel and the row gets cancelled.
    When you are using it you have to cancel things on your own...

    I'm wondering if we are not the only ones using that feature seeing how much not stable it is...

    Also, the final users do not want to insert their new items using the insert new row feature.
    They want a dialog that pops up or something similar and let them insert the new element. They do not want to do it inline.
    They call my grid the Excel grid... That feature can work for technical guys like me but not the end user.

    That means that I stop using that feature for good. Maybe for now...
    Thank you for your help.

    John.
Back to Top
DevCraft banner