IsPrimaryKey

Goodmorning,

In a grid i have to use a model with two keys. (Id and SiteId).


After editing the Time the grid change the SiteId of records that contains the same Id value.



Second problem is that after editing the DateTime of any record 



and save it it add 5 hours instead of 30 min.



I will attach the source of it.

Best regards,

Thijs van Rijswijk

Attachment: OnCommandClicked_83371d91.zip

7 Replies

RS Renjith Singh Rajendran Syncfusion Team April 15, 2020 12:08 PM UTC

Hi Thijs, 

Query 1 : After editing the Time the grid change the SiteId of records that contains the same Id value. 
We would like to inform you that, to perform CRUD operation in Grid, it is a must to set a unique value column as "IsPrimaryKey"  for Grid. Based on the primary key column value only we will perform CRUD in Grid. So as two rows have same primary key value, will update the edited data to the rows having the same primary value. So it is suggested to set a unique value column as primary key to perform CRUD operations in Grid. 
 
And if you don’t want to edit the SiteId column in Grid, then you can set the column level AllowEditing property to false, instead of setting the IsPrimaryKey for the column.  

Please refer the code below, 

 
    <GridColumns> 
        <GridColumn Field=@nameof(TimeTransactionsDto.TranId) IsPrimaryKey="true" HeaderText="Id" TextAlign="TextAlign.Left" Edit="false"></GridColumn> 
        <GridColumn Field=@nameof(TimeTransactionsDto.SiteId) AllowEditing="false" HeaderText="SiteId" TextAlign="TextAlign.Left" Edit="false"></GridColumn> 
        <GridColumn Field=@nameof(TimeTransactionsDto.DateTimeWeek) HeaderText="Week" TextAlign="TextAlign.Left" Edit="false"></GridColumn> 
        ... 
   </GridColumns> 


We have also modified the shared sample, we are attaching the sample for your convenience, please downalod the sample from the link below, 
 
Query 2 : Second problem is that after editing the DateTime of any record and save it it add 5 hours instead of 30 min. 
We suggest you to add the hours based on the delay(for our case delay is -1 so added 1 hour extra, as for your case its -10 you can add 10) in time in the OnActionBegin event handler to overcome the problem you are facing. Please use the code below, 

 
        public void ActionBeginHandler(ActionEventArgs<TimeTransactionsDto> args) 
        { 
            if (args.Type == "actionBegin") 
                if (args.Action == "edit") 
                { 
                    if (args.RequestType == Syncfusion.Blazor.Grids.Action.Save) 
                    { 
                        args.Data.DateTime = args.Data.DateTime.Value.AddHours(-5);   //based on your delay please set the value. Based on your delay(-4 or -5) please add the hours 
                        var d = args.Data; 
                    } 
                } 
        } 

    public class TimeTransactionsDto 
    { 
        public int TranId { get; set; } 
        ... 
       public DateTime? DateTime { get; set; } 
        ... 
   } 


Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



TV Thijs van Rijswijk April 15, 2020 11:07 PM UTC

Hi Renjith,

So it's impossible to have a combined primairy key of two fields?

Best regards,

Thijs van Rijswijk


RS Renjith Singh Rajendran Syncfusion Team April 16, 2020 09:36 AM UTC

Hi Thijs, 

Thanks for your update. 

Currently we don’t have support for multiple primary key column in Grid. But we have considered this requirement as a feature and logged a feature task “Provide multiple primary key column support in Grid” for this and added to our feature request list. Based on specific parameters including product vision and technological feasibility we will implement this feature. This will be available in any of our upcoming releases.  

You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through this link.  

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran. 



TV Thijs van Rijswijk April 17, 2020 12:54 AM UTC

Hi Renjith,

Ok thank you for the response.

Did make a workaround for now.

Best regards,

Thijs 


RS Renjith Singh Rajendran Syncfusion Team April 17, 2020 02:29 PM UTC

Hi Thijs, 

We can workaround this requirement by using the “Custom Way of Binding” data to Grid. In this way. With this, we can define our own customized Data Operations for Grid. So by using this, we can fetch the row to be edited based on the two primary key values(TranId and SiteId) and update values for that data row. Please refer the below documentation for more details. 
 
In the Update method of CustomAdaptor, we suggest you to fetch the current edited row data based on the values of both the primary key fields(TranId and SiteId) and update the values. Please refer the code below, 

 
            public override object Update(DataManager dm, object value, string keyField, string key) 
            { 
                var data = dto.Where(or => or.TranId == (value as TimeTransactionsDto).TranId && or.SiteId == (value as TimeTransactionsDto).SiteId).FirstOrDefault(); 
                if (data != null) 
                { 
                    data.TranId = (value as TimeTransactionsDto).TranId; 
                    data.SiteId = (value as TimeTransactionsDto).SiteId; 
                    data.DateTimeWeek = (value as TimeTransactionsDto).DateTimeWeek; 
                    data.Activity = (value as TimeTransactionsDto).Activity; 
                    data.DateTime = (value as TimeTransactionsDto).DateTime; 
                } 
                return value; 
            } 


We have also modified the sample for your convenience. Please download the sample from the link below, 
 
Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



MW Muhammad Waleed March 3, 2024 07:46 AM UTC

how to provide primary key for a column when the grid is being auto-generated and i can't manually declare/set using attribute of primarykey=true



MS Monisha Saravanan Syncfusion Team March 4, 2024 11:43 AM UTC


Hi Muhammad,


Greetings from Syncfusion.


Query: “how to provide primary key for a column when the grid is being auto-generated and i can't manually declare/set using attribute of primarykey=true”


We recommend you to set the IsPrimaryKey property of the column by using OnDataBound event. The OnDataBound event will be triggered before data is bound to the Grid. So we can customize based on our requirement inside the event handler.


We have already documented this topic in this section. Kindly refer the mentioned thread and code snippet for further followups.



<SfGrid @ref="Grid" TValue="OrderData" DataSource="@Orders">

    <GridEvents OnDataBound="DataBoundHandler" TValue="OrderData"></GridEvents>

    <GridEditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true"></GridEditSettings>

</SfGrid>

 

@code {

    private SfGrid<OrderData> Grid;

    public List<OrderData> Orders { get; set; }

      

    protected override void OnInitialized()

    {

        Orders = OrderData.GetAllRecords();

    }  

    public void DataBoundHandler(BeforeDataBoundArgs<OrderData> args)

    {

        Grid.Columns[0].IsPrimaryKey = true;

    }

}

 


Kindly get back to us if you have further queries.


Regards,

Monisha


Loader.
Up arrow icon