res for tty 无法 bind void type reat
(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
来源: http://www.bubuko.com/infodetail-2077986.html