New Posts New Posts RSS Feed: Add new row in a DataGrid
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Add new row in a DataGrid

 Post Reply Post Reply
Author
danjal View Drop Down
Groupie
Groupie


Joined: 20-Sep-2010
Posts: 43
Post Options Post Options   Quote danjal Quote  Post ReplyReply Direct Link To This Post Topic: Add new row in a DataGrid
    Posted: 15-Mar-2011 at 10:13am

I have a RelatedEntityList that is bound to the ItemsSource of a C1DataGrid.

ItemsSource="{Binding p_vm.p_currentItem.p_actions, Mode=TwoWay, Source={StaticResource ViewModelLocator}}"

 

The C1DataGrid has a „Add new row“ row.

 

When I want to create a new row, I enter into the „Add new row“ row. The grid´s event BeginningNewRow is fired:

public void actions_BeginningNewRow(object sender, DataGridBeginningNewRowEventArgs e)

        {

            EntMyAction action = (e.Item as EntMyAction);

            action.p_type = 13;

        }

 

I do not have to create a new  EntMyAction - that is somehow done for me. My new EntMyAction has id=0 and EntityState is Detached.

 

When I am done with my row, the grid´s event CommittingNewRow is fired:

public void actions_CommittingNewRow(object sender, DataGridEndingNewRowEventArgs e)

        {

            EntMyAction action = e.NewRow.DataItem as EntMyAction;

            // What should I do here?

        }

 

After the CommittingNewRow event, I can see that my new EntMyAction gets id=-100 and EntityState is Added, but I do not see it in my grid.

 

If I try the same procedure again, I get an error after the CommittingNewRow event:

An entity with this key: EntMyAction: 0 already exists in this EntityManager

 

What is going on?

How do I work with DevForce and the “Add new row” in a C1DataGrid?

 

 

 

Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 31-Mar-2011 at 1:11am

Hi Danjal,

 
Regarding the new entity not showing in the grid:
You are binding to a RelatedEntityList, so you must set the relation entity or FK.
However, it looks like assigning values in the DataGridEventArgs is not updating the entity itself. You can see that the entity was Added, but all values you assigned in code (i.e. action.p_type = 13;) are actually null. This might be a bug in C1DataGrid. (you might want to check C1DataGrid documentation and/or report it to ComponentOne)
 
 
Regarding the "An entity with this key: EntMyAction: 0 already exists in this EntityManager" error:
I suggest generating the id of the entity. (i.e. mgr.GenerateID(entity, Entity.PropertyMetadata.EntityID); )
Remember that I mentioned earlier that values assigned to the DatagridEventArgs are not reflected in the entity itself, you you will need to get a hold of the entity to be able to properly assign values:
 
    private void c1DataGrid_CommittedNewRow(object sender, C1.Silverlight.DataGrid.DataGridRowEventArgs e) {
      // this will not work:
      //_mgr.GenerateId((Order)e.Row.DataItem, Order.PropertyMetadata.OrderID);
      // working directly with the entity will work:
      var query = EntityQueryBuilder.BuildQuery(new EntityKey[] { ((Order)e.Row.DataItem).EntityAspect.EntityKey });
      Order order = (Order)query.With(QueryStrategy.CacheOnly).FirstOrNullEntity();
      _mgr.GenerateId(order, Order.PropertyMetadata.OrderID);
    }
 
 
While working with C1DataGrid I also found out that calling SaveChangesAsync() will cause an "IdGenerator problem - not all temp ids converted to permanent ids" error. Calling ForceIdFixupAsync() will cause the same issue. A workaround is doing a partial save:
 
    var entitiesToSave = _mgr.FindEntities(EntityState.AnyAddedModifiedOrDeleted);
    _mgr.SaveChangesAsync(retrievedEntities);
 
or, if you want to save the new row immediatelly after commiting it:
 
    private void c1DataGrid_CommittedNewRow(object sender, C1.Silverlight.DataGrid.DataGridRowEventArgs e) {
      // code as posted above
      // ...
      _mgr.SaveChangesAsync(new Order[] { order });
    }
 
Regards,
   Silvio.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down