Dev GridControl

Posted 技术学习

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
        }

以上是关于Dev GridControl的主要内容,如果未能解决你的问题,请参考以下文章

>&/dev/null和>/dev/null 2>&1区别(>/dev/null 2>&1> /dev/null 2>&1)

>&/dev/null和>/dev/null 2>&1区别(>/dev/null 2>&1> /dev/null 2>&1)

/dev/tty /dev/pts

/dev/null与/dev/zero区别

机器视觉dev_update_on和dev_update_off算子

hostapd/dev/random/dev/urandom