We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Add new row manually

Hi, I am trying to figure out how to commit a new row manually, I found this example for WPF but it does not work for winforms.

https://www.syncfusion.com/kb/5099/how-to-do-addnewrow-operations-programmatically-in-the-sfdatagrid

The other way I can do it is cancel the AddNewRowInitiating event then add the new row to the bound data, but I can't find a way to cancel it either.

Thanks



1 Reply

DY Deivaselvan Y Syncfusion Team November 29, 2018 12:03 PM UTC

Hi Chris, 

Thanks for contacting Syncfusion support. 

You can perform the AddNewRow operations programmatically same as the example provided in WPF. Please refer to the following code example and sample from the given location. 

Code example :  

private void commit_Click(object sender, EventArgs e) 
{ 
    var rowIndex = this.sfDataGrid.CurrentCell != null ? this.sfDataGrid.CurrentCell.RowIndex : 0; 
    if (this.sfDataGrid.IsAddNewRowIndex(rowIndex)) 
    { 
        if (this.sfDataGrid.CurrentCell.IsEditing) 
            this.sfDataGrid.CurrentCell.EndEdit(true); 
        var rowColumnIndex = new RowColumnIndex(this.sfDataGrid.CurrentCell.RowIndex, this.sfDataGrid.CurrentCell.ColumnIndex); 
 
        var gridModel = ReflectionHelper.GetProperty(typeof(SfDataGrid), "GridModel").GetValue(sfDataGrid, null); 
        var addNewRowController = ReflectionHelper.GetProperty(gridModel.GetType(), "AddNewRowController").GetValue(gridModel, null); 
        var commitAddNewMethod = ReflectionHelper.GetMethod(addNewRowController.GetType(), "CommitAddNew"); 
 
        ReflectionHelper.Invoke(commitAddNewMethod, addNewRowController, new object[] { true }); 
        rowColumnIndex.RowIndex = this.sfDataGrid.GetAddNewRowIndex(); 
        this.sfDataGrid.SelectedItems.Clear(); 
        if (this.sfDataGrid.AddNewRowPosition == RowPosition.Top) 
            rowColumnIndex.RowIndex = rowColumnIndex.RowIndex + 1; 
        this.sfDataGrid.MoveToCurrentCell(rowColumnIndex); 
    } 
} 
 
private void cancel_Click(object sender, EventArgs e) 
{ 
    if (this.sfDataGrid.View.IsAddingNew) 
    { 
        if (this.sfDataGrid.CurrentCell.IsEditing) 
            this.sfDataGrid.CurrentCell.EndEdit(true); 
 
        var gridModel = ReflectionHelper.GetProperty(typeof(SfDataGrid), "GridModel").GetValue(sfDataGrid, null); 
        var addNewRowController = ReflectionHelper.GetProperty(gridModel.GetType(), "AddNewRowController").GetValue(gridModel, null); 
        var cancelAddNewMethod = ReflectionHelper.GetMethod(addNewRowController.GetType(), "CancelAddNew"); 
 
        ReflectionHelper.Invoke(cancelAddNewMethod, addNewRowController, new object[] { }); 
         
    } 
} 
 

Regards, 
Deivaselvan 


Loader.
Live Chat Icon For mobile
Up arrow icon