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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Add new row manually

Thread ID:

Created:

Updated:

Platform:

Replies:

141186 Nov 28,2018 07:33 PM UTC Nov 29,2018 12:03 PM UTC WinForms 1
loading
Tags: SfDataGrid
Phunction
Asked On November 28, 2018 07:33 PM UTC

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



Deivaselvan Y [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;