How to set default values for columns AddNewRow in WPF DataGrid?
columns in add new rowAddNewRow allows you to add rows in the runtime to enter new records in the DataGrid control. By default, SfDataGrid does not set any default value for columns in the AddNewRow. However, the SfDataGrid allows you to set some initial values for AddNewRow by clicking in the AddNewRow and handling the AddNewRowInitiating event that is triggered when you start editing in AddNewRow. By handling this event, you can add custom values for the AddNewRow columns in the DataGrid.
The following code example demonstrates how to add initial values to AddNewRow by handling the AddNewRowInitiating event.
C#
this.dataGrid.AddNewRowInitiating += dataGrid_AddNewRowInitiating; private void dataGrid_AddNewRowInitiating(object sender, AddNewRowInitiatingEventArgs args) { Random random = new Random(); var data = args.NewObject as OrderInfo; data.OrderID = random.Next(10101,10300); data.CustomerID = "Customer"+random.Next(1,30); data.ShippingDate = DateTime.UtcNow; data.EmployeeID = random.Next(1, 10); data.ShipCity = "Chennai"; }
Event | Parameters | Description |
AddNewRowIntiating | NewObject | This event is triggered when you start editing in the AddNewRow. NewObject – This contains the newly created object that helps set the default value when you start editing in the AddNewRow. Note: NewObject is read-only property. |
The following screenshot shows the output of the above code. The AddNewRow is initiated with some default values.
Sample Links:
Conclusion
I hope you enjoyed learning about how to set default values for columns AddNewRow in WPF Grid.
You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Grid documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!