很多人问到在使用DevExpress GridControl控件时,在使用了 gridView1.AddNewRow()后,为什么gridView1_InitNewRow不触发?

网上有很多解释,总结起来大概有如下三条:

1、 gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

2、 gridView1.OptionsBehavior.Editable = true;

3、  GridControl必须要用以下形式的绑定数据源。

BindingSource bdSource = new BindingSource();

bdSource.DataSource = InitDt();
this.gridControl1.DataSource = bdSource;

其实主要原因和这些都没有关系。

真正的原因是GridControl.DataSource=数据源。哪怕是没有记录的数据源。否则gridView1_InitNewRow是不会触发的。

下面是本文的测试Demo代码:注释掉多余的部分,程序照样测试通过。

 public partial class Form1 : Form
    {
        //BindingSource bdSource = new BindingSource();
        public Form1()
        {
            InitializeComponent();
            //bdSource.DataSource = InitDt();
            this.gridControl1.DataSource = InitDt(); 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            //gridView1.OptionsBehavior.Editable = true;
            gridView1.AddNewRow();
        }

        private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            MessageBox.Show("触发事件测试");
        }
        private DataTable InitDt()
        {
            DataTable dt = new DataTable("个人简历");
            //dt.Columns.Add("id", typeof(int));
            //dt.Columns.Add("name", typeof(string));
            //dt.Columns.Add("sex", typeof(int));
            //dt.Columns.Add("address", typeof(string));
            //dt.Columns.Add("aihao", typeof(string));
            //dt.Columns.Add("photo", typeof(string));
            //dt.Rows.Add(new object[] { 1, "张三", 1, "东大街6号", "看书", "" });
            //dt.Rows.Add(new object[] { 1, "王五", 0, "西大街2号", "上网,游戏", "" });
            //dt.Rows.Add(new object[] { 1, "李四", 1, "南大街3号", "上网,逛街", "" });
            //dt.Rows.Add(new object[] { 1, "钱八", 0, "北大街5号", "上网,逛街,看书,游戏", "" });
            //dt.Rows.Add(new object[] { 1, "赵九", 1, "中大街1号", "看书,逛街,游戏", "" });
            return dt;
        }

}

视图:关于DevExpress GridControl中gridView1_InitNewRow事件不触发的原因

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-04
  • 2022-12-23
  • 2021-04-08
  • 2022-12-23
  • 2022-01-20
  • 2021-09-05
  • 2021-10-01
相关资源
相似解决方案