Skip to main content

GridView.AddNewRow() Method

Adds a new record to the GridView.

Namespace: DevExpress.XtraGrid.Views.Grid

Assembly: DevExpress.XtraGrid.v24.2.dll

Declaration

public override void 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.

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; }
    }
}

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.

See Also