Articles in this section
Category / Section

How to do AddNewRow operations programmatically in WPF DataGrid?

3 mins read

In the SfDataGridAddNewRow allows you to add the new data row in the UI by entering new records. By default, the new row is added at the end, otherwise it is added based on the sorted data position when LiveUpdateMode is set as AllowDataShapping.

The following screenshot shows the default view of AddNewRow in the SfDataGrid.

 

http://www.syncfusion.com/uploads/user/kb/wpf/wpf-20241/wpf-20241_img1.png

Figure 1 : Default view AddNewRow

When adding a new row, you can commit the new row by pressing the Enter key and cancel committing the new row by pressing the ESC key twice.

 

You can perform these operations programmatically in run time also.

The following code example illustrates how to cancel the new row being committed by calling AddNewRowController.CancelAddNew method.

C#

private void cancel_Click(object sender, RoutedEventArgs e)
{
    //Check whether the data is newly added 
    if (this.sfdatagrid.View.IsAddingNew)
    {
        // Which end edit the current cell. by passing false, it revert the entered value.
        if (this.sfdatagrid.SelectionController.CurrentCellManager.CurrentCell.IsEditing)
            this.sfdatagrid.SelectionController.CurrentCellManager.EndEdit(true);
        var addNewRowController = this.sfdatagrid.GetAddNewRowController();
        addNewRowController.CancelAddNew();
    }
} 

Similarly, you can also commit the new row programmatically using CommitAddNew method.

C#

private void commit_Click(object sender, RoutedEventArgs e)
{
    // Which avoid the process of committing normal rows. It only processed for AddNewRow.
    var rowIndex = this.sfdatagrid.SelectionController.CurrentCellManager.CurrentCell != null ? this.sfdatagrid.SelectionController.CurrentCellManager.CurrentCell.RowIndex : 0;
    if (this.sfdatagrid.IsAddNewIndex(rowIndex))
    {
        // Which end edit the current cell. by passing false, it commit the entered value.
        if (this.sfdatagrid.SelectionController.CurrentCellManager.CurrentCell.IsEditing)
            this.sfdatagrid.SelectionController.CurrentCellManager.EndEdit(true);
 
        //Initialize row and column index after committing new row 
        rowColumnIndex = this.sfdatagrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex;
        var addNewRowController = this.sfdatagrid.GetAddNewRowController();
        addNewRowController.CommitAddNew();
        rowColumnIndex.RowIndex = addNewRowController.GetAddNewRowIndex();
        this.sfdatagrid.SelectedItems.Clear();
        //If the AddNewRowPosition is Top need to move the current cell to next row 
        if (this.sfdatagrid.AddNewRowPosition == AddNewRowPosition.Top)
            rowColumnIndex.RowIndex = rowColumnIndex.RowIndex + 1;
        // Which retains the current cell border in the row after canceling addnewrow as you press esc key operation.
        this.sfdatagrid.MoveCurrentCell(rowColumnIndex);
    }
}

 

Conclusion

I hope you enjoyed learning about how to use image path as an image source in GridImageColumn.


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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied