Articles in this section
Category / Section

How to commit the AddNewRow value without pressing enter key in WinForms DataGrid?

1 min read

In WinForms DataGrid, 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::

C# AddNewRow_CS

VB : AddNewRow_VB

 

Conclusion

I hope you enjoyed learning about how to commit the AddNewRow value in WinForms DataGrid.

You can refer to our WinForms 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 WinForms 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 (0)
Please sign in to leave a comment
Access denied
Access denied