How to allow user to add a new row in WPF DataGrid (SfDataGrid)?
WPF DataGrid (SfDataGrid) provides built-in row called AddNewRow. It allows user to add a new row to underlying collection. You can enable or disable by setting SfDataGrid.AddNewRowPosition property. You can also set the position of built-in AddNewRow through AddNewRowPosition property.
When you start editing in AddNewRow, the WPF DataGrid control creates an instance for the underlying data object and adds it to underlying collection when editing completed. So, the underlying data object must be defined with default constructor.
You can commit or cancel a new row from being added by pressing the Enter and Esc key respectively.
<syncfusion:SfDataGrid x:Name="dataGrid"
ItemsSource="{Binding Orders}"
AddNewRowPosition="Top">
</syncfusion:SfDataGrid>
Add a new row programmatically to WPF DataGrid
You can add a new row by using SfDataGrid.View.AddNew and SfDataGrid.View.CommitNew methods.
private void Button_Click(object sender, RoutedEventArgs e)
{
dataGrid.View.AddNew();
this.dataGrid.View.CommitNew();
}
Customize default string of built-in AddNewRow
WPF DataGrid enables you to customize the watermark text of AddNewRow by changing value of AddNewRowText in Resource Designer.
You can go through this user guide to know more about customizing default string of AddNewRow’s text.
Initializing default values for AddNewRow
WPF DataGrid allows you to set the default values for AddNewRow while initiating through AddNewRowInitiatingEventArgs.NewObject property in SfDataGrid.AddNewRowInitiating event.
dataGrid.AddNewRowInitiating += DataGrid_AddNewRowInitiating;
private void DataGrid_AddNewRowInitiating(object sender, AddNewRowInitiatingEventArgs e)
{
var data = e.NewObject as OrderInfo;
data.OrderID = 1000;
data.CustomerID = "BERGS";
data.CustomerName = "Hanna";
data.Country = "UK";
data.ShipCity = "London";
}
Take a moment to peruse the documentation, where you can find about built-in AddNewRow feature, with code examples.
Please refer this link to know about the essential features of Syncfusion WPF DataGrid.
Conclusion
I hope you enjoyed learning about how to allow user to add a new row in WPF DataGrid (SfDataGrid).
You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example 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!