Articles in this section
Category / Section

How to do AddNewRow operations programmatically in WinRT DataGrid?

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

 

 

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