How can I initialize an item for AddRecordAsync()?
I want to configure certain properties of my item so that it opens in the editor within the SfGrid. Using AddRecordAsync with the initial item does not trigger the edit dialog.
Hi Yongkee Cho,
Query: How can I initialize an item for AddRecordAsync() ?
We would like to let you know, there are two ways to use the AddRecordAsync() method in Syncfusion Blazor DataGrid:
If you want to open the editor within the
SfGrid for adding a new record, you should use the AddRecordAsync()
method without passing any data:
Concern Code Snippet:
|
SfGrid<Order> grid; public async Task OnClick() { await grid.AddRecordAsync(); } |
If you want to directly add a record with pre-populated data without opening the editor, you would use:
Concern Code Snippet:
|
Order adddata = new Order() { OrderID = 1000, CustomerID = "MJDGX", OrderDate = DateTime.Now, Freight = 3.01 };
await this.Grid.AddRecordAsync(adddata); |
We have prepared a simple example sample containing both the approaches, please check for your reference.
Concern Playground Sample:
For Additional Reference:
Regards,
Sanjay Kumar Suresh
I want to open the editor with pre-populated data, because some properties doesn't need to be input by user. I know we can initialize some properties using GridEditSettings template. But it updates the properties whenever I tried to edit while I want to update only when creation. In your SDK, AddRecordAsync and StartEditorAsync do not support the scenario when a datagrid opens editor with pre-populated data for creation.
<GridEditSettings>
<Template>
@{
var bug = context as Bug;
bug.ReportedAt = DateTime.UtcNow;
bug.ReportedBy = _email;
<BugEdit Content="@bug" />
}
</Template>
<GridEditSettings>
Hi Yongkee Cho,
While reviewing your query, it appears that you would like to set default values in the Add form when performing an add action on the Grid. We have documented this requirement in detail at the link below. Kindly review it and let us know if you need any further assistance.
Concern Documentation link:
Regards,
Sanjay Kumar Suresh
Hi Sanjay. My request is not the DefaultValue of a column (property) of an object. If I set a DefaultValue of a property, it will be overwritten by the DefaultValue each time I edit a row, which is not desired.
I want to open the editor with a record with pre-populated data and it seems not supported in your SDK.
Hi Yongkee Cho,
Greetings from Syncfusion,
Based on your query, the default value is supported only when the row is in add mode. This is the default behavior.
However, in edit mode, the grid displays the existing data in the edit form, so the default value is not shown.If you want to override this behavior and show the default value during editing,we suggest using the RowUpdating event to set the cell value to the default during editing Please refer to the attached code snippet and sample for your reference.
<SfGrid DataSource="@Orders" Height="315" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })"> <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings> <GridEvents RowEditing="RowEditingHandler" TValue="Order"></GridEvents> <GridColumns> <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" Width="120"></GridColumn> <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="120" DefaultValue="@("ANTON")"></GridColumn> <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" EditType="Syncfusion.Blazor.Grids.EditType.DatePickerEdit" Format="d" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right" Width="130" Type="Syncfusion.Blazor.Grids.ColumnType.Date"></GridColumn> <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right" Width="120"></GridColumn> </GridColumns> </SfGrid>
@code { public List<Order> Orders { get; set; }
public void RowEditingHandler(RowEditingEventArgs<Order> args) { args.Data.CustomerID = "Default"; }
public class Order { public int? OrderID { get; set; } public string CustomerID { get; set; } public DateTime? OrderDate { get; set; } public double? Freight { get; set; } } }
|
Regards,
Guhanathan R
- 5 Replies
- 3 Participants
-
YC Yongkee Cho
- Apr 24, 2025 07:01 PM UTC
- May 1, 2025 04:33 AM UTC