Skip to main content

How to: Assign Different Column Editors for Different Rows

This example shows how to assign different in-place editors (CalcEdit, SpinEdit and TextEdit) to different grid cells by handling the GridView.CustomRowCellEdit event.

CustomRowCellEdit-example

public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        List<MyRecord> dataSource = new List<MyRecord>();
        RepositoryItemSpinEdit riSpinEdit = new RepositoryItemSpinEdit();
        RepositoryItemCalcEdit riCalcEdit = new RepositoryItemCalcEdit();
        RepositoryItemTextEdit riTextEdit = new RepositoryItemTextEdit();
        RepositoryItem[] inplaceEditors;


        private void Form1_Load(object sender, EventArgs e) {
            inplaceEditors = new RepositoryItem[] { riSpinEdit, riTextEdit, riCalcEdit };

            GridControl gridControl1 = new GridControl();
            this.Controls.Add(gridControl1);
            gridControl1.Dock = DockStyle.Fill;

            for (int i = 0; i < 3; i++)
                dataSource.Add(new MyRecord((i+1) * i, (i+1) * i + 10, (i+1) * i + 20));
            gridControl1.DataSource = dataSource;

            gridControl1.RepositoryItems.Add(riSpinEdit);
            gridControl1.RepositoryItems.Add(riCalcEdit);
            gridControl1.RepositoryItems.Add(riTextEdit);

            GridView gridView1 = gridControl1.MainView as GridView;
            gridView1.CustomRowCellEdit += GridView1_CustomRowCellEdit;
            gridView1.OptionsView.ShowButtonMode = DevExpress.XtraGrid.Views.Base.ShowButtonModeEnum.ShowAlways;

            riTextEdit.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            riTextEdit.DisplayFormat.FormatString = "c2";


        }

        private void GridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) {
            GridView view = sender as GridView;
            int editorIndex = (view.GetDataSourceRowIndex(e.RowHandle) + e.Column.VisibleIndex) % 3;
            e.RepositoryItem = inplaceEditors[editorIndex];
        }
    }

    public class MyRecord {
        public MyRecord(int val1, int val2, int val3) {
            Column1 = val1;
            Column2 = val2;
            Column3 = val3;
        }
        public int Column1 { get; set; }
        public int Column2 { get; set; }
        public int Column3 { get; set; }
    }