GridView.AddNewRow() Method

Adds a new record to this GridView.

Namespace: DevExpress.XtraGrid.Views.Grid

Assembly: DevExpress.XtraGrid.v20.1.dll

Declaration

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


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