How to set value of a cell in new row

Hi,

I have a UserControl with SfDataGrid bound to a custom object collection.

When a new row is added to the datagrid, I need to set programmatically the value of a specific column. I see the datagrid creates a new object for the new row, yet cannot set the respective property of the object in the constructor, as at that moment the value is still unknown.

How can I get a reference to the newly created object bound to the new row in order to set its property programmatically?

Would an alternative solution be to set the value of a cell on a newly created row in the datagrid and, if yes, how can I do that?


Best regards,

Alexander


1 Reply

DM Dhanasekar Mohanraj Syncfusion Team May 17, 2023 01:41 PM UTC

Hi Alexander,

Your requirement to “Set the default value of a cell in AddNewRow” will be achievable by using the SfDataGrid.AddNewRowInitiating event as shown below,

this.dataGrid.AddNewRowInitiating += dataGrid_AddNewRowInitiating;

 

void dataGrid_AddNewRowInitiating(object sender, AddNewRowInitiatingEventArgs args)

{

    var data = args.NewObject as OrderInfo;

    data.OrderID = 101;

}


For more information related to Initializing default values for AddNewRow, please refer to the below user guide documentation link,

UG Link: https://help.syncfusion.com/wpf/datagrid/data-manipulation#initializing-default-values-for-addnewrow

Regards,

Dhanasekar M.

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Loader.
Up arrow icon