How to commit the AddNewRow value without pressing enter key in WinForms DataGrid?
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
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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!