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.


5 Replies

SK Sanjay Kumar Suresh Syncfusion Team April 25, 2025 06:09 AM UTC

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:

https://blazorplayground.syncfusion.com/embed/BDhyNfherjIXFQWf?appbar=true&editor=true&result=true&errorlist=true&theme=bootstrap5


For Additional Reference:

https://blazor.syncfusion.com/documentation/datagrid/editing#performing-crud-operations-programmatically


Regards,

Sanjay Kumar Suresh



YC Yongkee Cho April 25, 2025 10:57 AM UTC

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>


SK Sanjay Kumar Suresh Syncfusion Team April 28, 2025 10:27 AM UTC

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:

https://blazor.syncfusion.com/documentation/datagrid/editing#default-column-values-on-adding-new-record


Regards,

Sanjay Kumar Suresh



YC Yongkee Cho April 29, 2025 04:44 PM UTC

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.


 



GR Guhanathan Ramanathan Syncfusion Team replied to Yongkee Cho May 1, 2025 04:33 AM UTC

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; }

    }

}

 

 

sample:https://blazorplayground.syncfusion.com/embed/BDVIXzgZpUDWnrhC?appbar=true&editor=true&result=true&errorlist=true&theme=bootstrap5


Regards,
Guhanathan R


Loader.
Up arrow icon