Back to Devexpress

GridView.AddNewRow() Method

windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview-1b239616.md

latest7.2 KB
Original Source

GridView.AddNewRow() Method

Adds a new record to the GridView.

Namespace : DevExpress.XtraGrid.Views.Grid

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
public override void AddNewRow()
vb
Public Overrides Sub AddNewRow

Remarks

The AddNewRow method adds a new grid row at runtime. This method does not depend on the current ColumnViewOptionsBehavior.AllowAddRows property value. End users can also add new rows using the New Item Row and Data Navigator.

See ColumnView.AddNewRow to learn more.

The following sample illustrates how to add a new Data Grid row and instantly set its cell values. Also, see the Add and Remove Rows article for more examples.

csharp
using DevExpress.XtraGrid;
using System;
using System.ComponentModel;

namespace SampleGridApplication {
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        public Form1() {
            InitializeComponent();
            this.Load += Form1_Load;
            //end-users cannot add rows
            gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False;
        }

        private void Form1_Load(object sender, EventArgs e) {
            //load sample data base
            gridControl1.DataSource = SampleDS();
        }

        private void button1_Click(object sender, EventArgs e) {
            //add a new row
            gridView1.AddNewRow();
            //set a new row cell value. The static GridControl.NewItemRowHandle field allows you to retrieve the added row
            gridView1.SetRowCellValue(GridControl.NewItemRowHandle, gridView1.Columns["Name"], "Please enter new value");
        }

        //sample data source
        public BindingList<Entry> SampleDS() {
            BindingList<Entry> ds = new BindingList<Entry>();
            ds.Add(new Entry("One", 1));
            ds.Add(new Entry("Two", 2));
            ds.Add(new Entry("Three", 3));
            ds.AllowNew = true;
            return ds;
        }
    }

    //sample data source entry
    public class Entry {
        public Entry() { }
        public Entry(string name, Int32 id) {
            Name = name; ID = id;
        }
        public string Name { get; set; }
        public Int32 ID { get; set; }
    }
}
vb
Imports DevExpress.XtraGrid
Imports System
Imports System.ComponentModel

Namespace SampleGridApplication
    Partial Public Class Form1
        Inherits DevExpress.XtraEditors.XtraForm
            Public Sub New()
                InitializeComponent()
                AddHandler Me.Load, AddressOf Form1_Load
                'end-users cannot add rows
                gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False
            End Sub

            Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
                'load sample data base
                gridControl1.DataSource = SampleDS()
            End Sub

            Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
                'add a new row
                gridView1.AddNewRow()
                'set a new row cell value. The static GridControl.NewItemRowHandle field allows you to retrieve the added row
                gridView1.SetRowCellValue(GridControl.NewItemRowHandle, gridView1.Columns("Name"), "Please enter new value")
            End Sub

            'sample data source
            Public Function SampleDS() As BindingList(Of Entry)
                Dim ds As New BindingList(Of Entry)()
                ds.Add(New Entry("One", 1))
                ds.Add(New Entry("Two", 2))
                ds.Add(New Entry("Three", 3))
                ds.AllowNew = True
                Return ds
            End Function
    End Class

    'sample data source entry
    Public Class Entry
        Public Sub New()
        End Sub

        Public Sub New(ByVal name As String, ByVal id As Int32)
            Me.Name = name
            Me.ID = id
        End Sub
        Public Property Name() As String
        Public Property ID() As Int32
    End Class
End Namespace

The following code snippets (auto-collected from DevExpress Examples) contain references to the AddNewRow() method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-grid-auto-expand-new-master-row/CS/Q205071/Form1.cs#L56

csharp
GridView childView = (GridView)view.GetDetailView(e.RowHandle, e.RelationIndex);
childView.AddNewRow();
BeginInvoke(new FocusNewRowDelegate(FocusNewRow), new object[] { childView });

winforms-grid-auto-add-new-row-after-editing-last-cell/CS/Form1.cs#L92

csharp
{
    _View.AddNewRow();
    _View.FocusedColumn = _View.VisibleColumns[0];

winforms-grid-auto-expand-new-master-row/VB/Q205071/Form1.vb#L58

vb
Dim childView As GridView = CType(view.GetDetailView(e.RowHandle, e.RelationIndex), GridView)
childView.AddNewRow()
BeginInvoke(New FocusNewRowDelegate(AddressOf FocusNewRow), New Object() {childView})

winforms-grid-auto-add-new-row-after-editing-last-cell/VB/Form1.vb#L85

vb
Private Function AddNewRow() As Boolean
    _View.AddNewRow()
    _View.FocusedColumn = _View.VisibleColumns(0)

See Also

GridView Class

GridView Members

DevExpress.XtraGrid.Views.Grid Namespace