We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Activator.CreateInstance<TValue> still called after ActionBegin(ActionEventArgs<ChartOfAccountInfoPersist> arg) invoked

I am trying to use the grid. I am using CSLA for my business objects.  I cannot use the Activator.CreateInstance----ever.  I have to use a method on the class via a portal.   This is really simple stuff, but yet simply not working:

<SfGrid DataSource="@vm.Model" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })">
        <GridEvents TValue="ChartOfAccountInfoPersist" OnActionBegin="ActionBegin"></GridEvents>
        <GridSelectionSettings Type="SelectionType.Single" Mode="Syncfusion.Blazor.Grids.SelectionMode.Row"></GridSelectionSettings>
        <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" ShowDeleteConfirmDialog="true" Mode="EditMode.Normal"></GridEditSettings>
        <GridColumns>
            <GridColumn Field=@nameof(ChartOfAccount.Id) HeaderText="Id" TextAlign="TextAlign.Left" AllowEditing="false" Width="160"></GridColumn>
            <GridColumn Field=@nameof(ChartOfAccount.AccountNumber) HeaderText="Account Number" TextAlign="TextAlign.Left" Width="160"></GridColumn>
            <GridColumn Field=@nameof(ChartOfAccount.Description) HeaderText="Description" TextAlign="TextAlign.Left" Width="160"></GridColumn>
            <GridColumn Field=@nameof(ChartOfAccount.ChargeOfAccountParentId) HeaderText="Parent" TextAlign="TextAlign.Left" Width="160"></GridColumn>
        </GridColumns>
    </SfGrid>


 public void ActionBegin(ActionEventArgs<ChartOfAccountInfoPersist> arg)
    {
        //Handles add operation
        if (arg.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Add))
        {
            CompanyEmployeeCriteria criteria = new CompanyEmployeeCriteria(1, 1);
            arg.Data = chartOfAccountPortal.Create(criteria);
        }


        This if statement is never true.
        if (arg.RequestType.Equals(Syncfusion.Blazor.Grids.Action.BeginEdit))
        {
            CompanyEmployeeCriteria criteria = new CompanyEmployeeCriteria(1, 1);
            arg.Data = chartOfAccountPortal.Create(criteria);
            arg.Data.AccountNumber = arg.RowData.AccountNumber;
            arg.Data.CompanyKey = arg.RowData.CompanyKey;
            arg.Data.Description = arg.RowData.Description;
            arg.Data.ChartOfAccountTypeCd = arg.RowData.ChartOfAccountTypeCd;
        }
    } <-- when the this executes, it calls Activator.CreateInstance which will return null on the object.  You can see in the code that I have already created the object and I'm attempting to assign it back to the grid.  But, the arg.RowData is null too.  

Questions:

How do I prevent Activator.CreateInstance from invoking?

Why is arg.RequestType.Equals(Syncfusion.Blazor.Grids.Action.BeginEdit)) not t


Attachment: ChartOfAccountListView.razor_aa5c9bb0.zip

21 Replies

MS Monisha Saravanan Syncfusion Team December 8, 2022 12:31 PM UTC

Hi Kevin,


Greetings from Syncfusion support.


We could see that in your shared code snippet you have not enabled IsPrimaryKey property. We would like to inform you that CRUD operation in Grid will take place only based on the unique PrimaryKey column value. So IsPrimaryKey property must be enabled to any one of the unique valued column defined in grid. Only based on primary key value, the CRUD will be properly performed in Grid. So kindly check the reported issue after using IsPrimaryKey for an unique column.


Kindly get back to us if you still face difficulties or if you have further queries at your end.


Regards,

Monisha



KC Kevin Cabral December 8, 2022 01:23 PM UTC

Are you sure?  Here is the code snippet that has the primary key identified.  I have not changed this.

<GridColumn Field=@nameof(ChartOfAccount.Id) IsPrimaryKey="true" HeaderText="Id" TextAlign="TextAlign.Left" AllowEditing="false" Width="160"></GridColumn>


KC Kevin Cabral December 8, 2022 01:26 PM UTC

<form class="form-horizontal" method="post">
    <div>
    <SfGrid @ref=Grid DataSource="@vm.Model" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })">
        <GridEvents TValue="ChartOfAccountInfoPersist" OnActionBegin="ActionBeginHandler" OnActionComplete="ActionCompleteHandler"></GridEvents>
        <GridSelectionSettings Type="SelectionType.Single" Mode="Syncfusion.Blazor.Grids.SelectionMode.Row"></GridSelectionSettings>
        <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" ShowDeleteConfirmDialog="true"></GridEditSettings>
        <GridTemplates>
            <EmptyRecordTemplate>
                <span>Loading Data.</span>
            </EmptyRecordTemplate>
        </GridTemplates>


        <GridColumns>
            <GridColumn Field=@nameof(ChartOfAccount.Id) IsPrimaryKey="true" HeaderText="Id" TextAlign="TextAlign.Left" AllowEditing="false" Width="160"></GridColumn>
            <GridColumn Field=@nameof(ChartOfAccount.AccountNumber) HeaderText="Account Number" TextAlign="TextAlign.Left" Width="160"></GridColumn>
            <GridColumn Field=@nameof(ChartOfAccount.Description) HeaderText="Description" TextAlign="TextAlign.Left" Width="160"></GridColumn>
            <GridColumn Field=@nameof(ChartOfAccount.ChargeOfAccountParentId) HeaderText="Parent" TextAlign="TextAlign.Left" Width="160"></GridColumn>
        </GridColumns>
    </SfGrid>
    </div>
</form>


KC Kevin Cabral replied to Monisha Saravanan December 8, 2022 01:43 PM UTC

Hope you're having a good day :-)



PS Prathap Senthil Syncfusion Team December 9, 2022 12:49 PM UTC

Thanks for update

We created a simple sample based on the reported issue while performing a normal edit action, but the reported issue did not occur at our end. We would also like to inform you that Grid uses Activator.CreateInstanceTValue>() to create or clone new record instances during add and edit operations, so the model class and any referenced complex type classes must have parameter less constructors defined. 
In some cases, custom logic is required to create a new object, or a new object instance cannot be created using Activator.CreateInstanceTValue> (). In such cases, you can manually provide model object instances via events.

Please see the attached screenshots and sample solutions for your reference.

1. Triggered before editing the operation image.



2. Args.RowData received the image shown below.