Dev GridControl

(1)、gridView.AddNewRow()

(2)、实现 gridView_InitNewRow 事件

注:使用泛型集合绑定数据源,在GridView中实现自动添加行时,AddNewRow()方法不起效。在获取数据行时,
GetDataRow()方法无法获取数据,如果使用gridcontrol用于只呈现数据可以使用泛型集合作为数据源,如果涉及到增、删、改建议使用
DataTable作为数据源,这样以上两个方法可以正常使用。

方法1(绑定DataTable作为数据源):

Step1:绑定数据源

  gridControl.DataSource=dataTable;

 

Step2:gridView_InitNewRow 事件

  DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
      //DataRow dr = this.gridView1.GetDataRow(this.gridView1.FocusedRowHandle);
      //初始化赋值
     view.UpdateCurrentRow();

Step3:添加行触发事件

  gridView.AddNewRow()

 

方法2(绑定List<T>作为数据源)

Step1:绑定数据源

  List<DepartmentInfo> source=GetDepartmentList();            //获取泛型数据列表

  gridControl.DataSource=new BindingList<DepartmentInfo>(source);    //泛型类型转换

 

Step2:gridView_InitNewRow 事件

  DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
      //DataRow dr = this.gridView1.GetDataRow(this.gridView1.FocusedRowHandle);
      //初始化赋值
     view.UpdateCurrentRow();

Step3:添加行触发事件

  gridView.AddNewRow()

 

 

方法2初始化赋值优化:

//获取类型默认值

public static object DefaultForType(Type targetType)
{
       return targetType.IsValueType? Activator.CreateInstance(targetType) : null;  
}

 

初始化--gridView_InitNewRow

        private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;

            object destinationRow = gridView1.GetFocusedRow();
            object sourceRow = gridView1.GetRow(0);     //获取数据源第一行作为模板原型
            System.Reflection.PropertyInfo[] propertyCollection = sourceRow.GetType().GetProperties();
            foreach (System.Reflection.PropertyInfo propertyInfo in propertyCollection)
            {
                //获取第一行数据
                //object aa = sourceRow.GetType().GetProperty(propertyInfo.Name).GetValue(sourceRow, null);
                object aa = DefaultForType(sourceRow.GetType());
                destinationRow.GetType().GetProperty(propertyInfo.Name).SetValue(destinationRow, aa, null);

            }

            view.UpdateCurrentRow();
        }

posted on 2017-05-19 19:35 飘扬De黑夜 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/volts0302/p/6880012.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值