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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to commit the AddNewRow value without pressing Enter key in WinForms DataGrid (SfDataGrid)?

Platform: WinForms |
Control: SfDataGrid |
Published Date: August 14, 2018 |
Last Revised Date: January 13, 2020
Tags: addnewrow

Adding new row

In SfDataGirid, you can programmatically force an add new row to commit the changes by calling the CommitNew method in SfDataGrid.View, and invalidate the AddNewRowit.

C#

private void CommitAddNew(object sender, System.EventArgs e)
{
    if (this.sfDataGrid.CurrentCell.RowIndex == this.sfDataGrid.GetAddNewRowIndex())
    {
        this.sfDataGrid.View.CommitNew();
        var rowGenerator = (RowGenerator)ReflectionHelper.GetValue(sfDataGrid.TableControl, "RowGenerator");
        var addNewRow = rowGenerator.Items.FirstOrDefault(item => item.RowType == RowType.AddNewRow);
        if (addNewRow != null)
            addNewRow.RowData = this.sfDataGrid.View.CurrentAddItem;
        this.sfDataGrid.TableControl.Invalidate(this.sfDataGrid.TableControl.GetRowRectangle(this.sfDataGrid.GetAddNewRowIndex(), false));
    }
}

 

VB

Private Sub CommitAddNew(ByVal sender As Object, ByVal e As System.EventArgs)
 If Me.sfDataGrid.CurrentCell.RowIndex = Me.sfDataGrid.GetAddNewRowIndex() Then
  Me.sfDataGrid.View.CommitNew()
  Dim rowGenerator = CType(ReflectionHelper.GetValue(sfDataGrid.TableControl, "RowGenerator"), RowGenerator)
  Dim addNewRow = rowGenerator.Items.FirstOrDefault(Function(item) item.RowType = RowType.AddNewRow)
  If addNewRow IsNot Nothing Then
   addNewRow.RowData = Me.sfDataGrid.View.CurrentAddItem
  End If
  Me.sfDataGrid.TableControl.Invalidate(Me.sfDataGrid.TableControl.GetRowRectangle(Me.sfDataGrid.GetAddNewRowIndex(), False))
 End If
End Sub
 

 

Sample links::

CS C# : AddNewRow_CS

VB : AddNewRow_VB

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

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

Up arrow icon

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

Live Chat Icon For mobile