• 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();
            }

  • 相关阅读:
    pins-模块内的代码及资源隔离方案
    Android Gradle defaultConfig详解及实用技巧
    实用抓包工具:whistle
    Gradle中的闭包
    Android Gradle 依赖配置:implementation & api
    Android Studio Run项目出现Failure [INSTALL_FAILED_TEST_ONLY]
    Android 8.0对隐式广播的进一步限制
    cookie 详解
    一分钟内搭建全web的API接口神器json-server详解
    高性能前端 art-template 模板
  • 原文地址:https://www.cnblogs.com/volts0302/p/6880012.html
Copyright © 2020-2023  润新知