Use DateTime Value from Parentgrid as DefaultValue in ChildGrid Create

1 Answer 143 Views
Grid
Felix
Top achievements
Rank 1
Iron
Felix asked on 13 Dec 2022, 12:25 PM

Hi,

I can use a ParentGrid Value in a Childgrid if I set the DefaultValue in the model to the TemplateField

     .Model(model => {
       model.Id(p => p.ALIASQLF);
       model.Field(f=> f.ALIASQLF).DefaultValue("#=ALIASQLF#"); //is a field in the parentgrid and will be set at new row
     })

 

But once I try to use a DateTime Value from the ParentGrid this approach does no longer work, as the Field expects a C# DateTime Value

Okay, just convert the Value to DateTime, right?

But the "#=FieldName#" Syntax does not seem to work as a parameter in a C# method

For Example, the following code will try to convert literally "#=ReplicationDateString#" instead of the datetime-string value:

.Model(model => {

  model.Id(p => p.ReplicationDate);
  model.Id(p=> p.VS_Jobnumber);
  model.Field(f => f.ReplicationDate).DefaultValue(DateTime.ParseExact("#=ReplicationDateString#", "yyyy-MM-dd", CultureInfo.InvariantCulture));
})

 

Then just convert it within kendo, aight?

.Model(model => {

  model.Id(p => p.ReplicationDate);
  model.Id(p=> p.VS_Jobnumber);
  model.Field(f => f.ReplicationDate).DefaultValue("#= kendo.parseDate("2022-02-02") #");
})

But the kendo.parseDate function always returns a string, so this did not work either, as it expects a C# DateTime.

 

Has anyone an Idea how to solve this problem? Is there a better approach?

1 Answer, 1 is accepted

Sort by
1
Accepted
Aleksandar
Telerik team
answered on 16 Dec 2022, 05:51 AM

Hi Felix,

Indeed, the DefaultValue method expects a parameter of the type of the model property, therefore passing a string will not work.

A possible approach I can suggest is handling the DetailInit event, getting a reference to the child Grid and using the setOptions method to set the default value for new items:

<script>
    function onDetailInit(e){
        var detailGrid = e.detailRow.find(".k-grid").getKendoGrid()
        
        detailGrid.setOptions({
            dataSource:{
                schema:{
                    model:{
                        fields:{
                            DetailGridDateTimeField:{
                                defaultValue: e.data.ParentGridDateTimeField
                            }
                        }
                    }
                }
            }
        })
    }
</script>

Let me know if this helps.

Regards,
Aleksandar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Felix
Top achievements
Rank 1
Iron
commented on 28 Dec 2022, 01:38 PM

Hi Aleks, this was exactly what I have been trying to implement and it works like a charm.

Thank you very much!

Tags
Grid
Asked by
Felix
Top achievements
Rank 1
Iron
Answers by
Aleksandar
Telerik team
Share this question
or