This is a migrated thread and some comments may be shown as answers.

New Record on Grid, Default FK value

1 Answer 78 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Cynthia
Top achievements
Rank 1
Cynthia asked on 22 Apr 2013, 07:58 PM
I've got a grid.  Each new row will has a default FK value that ties each row to the parent on the page item (master, child or master - detail where the page is the master, the grid is each child record).

How do I set the FK value so that when I add a new row using the grid, it knows what FK to bind each row too?

1 Answer, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 24 Apr 2013, 11:47 AM
Hello Cynthia,

It is possible to define the defaultValue of the field in the Model configuration. Actually the same approach is used in the foreign key demo page.
fields: {
    ProductID: { editable: false, nullable: true },
    ProductName: { validation: { required: true} },
    CategoryID: { field: "CategoryID", type: "number", defaultValue: 1 },
    UnitPrice: { type: "number", validation: { required: true, min: 1} }
}

This approach is suitable in cases when the defaultValue is not supposed to change at runtime.
If you need to support such scenario, I suggest you to hook up to the edit event of the grid, check whether the edited model isNew and set manually the value of the foreign key column.
edit: function(e) {
  if (!e.model.isNew()) {
    //set the value
  }
}

I hope this information will help.

Kind regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Cynthia
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Share this question
or