Skip to main content

ColumnView.AddNewRow() Method

Adds a new record.

Namespace: DevExpress.XtraGrid.Views.Base

Assembly: DevExpress.XtraGrid.v24.1.dll

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

Declaration

public virtual void AddNewRow()

Remarks

Use the AddNewRow method to add a new record(row) to the View’s underlying data source. The View reflects any changes made to the data source immediately and focuses the new record. However, it’s possible to prevent moving the record focus. For more information, see the Focusing Cells topic.

The AddNewRow method is only available for data sources that support the IBindingList interface. When a new record is added, the data source may initialize certain values (key fields, for instance). Record initialization can also be performed manually by handling the ColumnView.InitNewRow event.

New row can be accepted in a number of ways:

The row can be discarded as follows:

Immediately after a row is added, you can identify this row with the ColumnView.FocusedRowHandle or GridControl.NewItemRowHandle property. To learn how to access row data, see Modify and Validate Cell Values.

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

End-users can add new records via the navigator control. An embedded navigator can be enabled via the GridControl.UseEmbeddedNavigator property. For Grid Views, an end-user can also add new rows via the new item row.

Refer to the Add and Remove Rows topic for more details on adding and deleting records via code.

Note

Detail pattern Views do not contain data and they are never displayed within XtraGrid. So, the AddNewRow member must not be invoked for these Views. The AddNewRow member can only be used with Views that display real data within the Grid Control. Use the following methods to access these Views with which an end user interacts at runtime.

See Also