DevExpress XtraGrid AddNewRow

来源:互联网 发布:apache php jsp 编辑:程序博客网 时间:2024/05/09 16:00

DevExpress XtraGrid 添加新的一行时,数据源DataSource如果是DataTable可以用AddNewRow方法,然后UpdateCurrentRow。但是如果DataSource的来源是List<T>,用AddNewRow是不起作用的,这时候需要将List<T>转换为BindingList<T>,才可以采用AddNewRow。

List<T>  to BindingList<T>直接用构造函数就可以:

List<T> source=new List<T>();

BindingList<T> temp=new BindingList<T>(source);

然后通过反射逐个根据源数据行对新加行赋值

gvSelected.AddNewRow();
object destinationRow = gvSelected.GetFocusedRow();
object sourceRow = gvOriginal.GetRow(i);
System.Reflection.PropertyInfo[] propertyCollection = sourceRow.GetType().GetProperties();
 foreach (System.Reflection.PropertyInfo propertyInfo in propertyCollection)
  {
               destinationRow.GetType().GetProperty(propertyInfo.Name).SetValue(

destinationRow, sourceRow.GetType().GetProperty(propertyInfo.Name).GetValue(sourceRow,null), null);
  }
 gvSelected.UpdateCurrentRow();

原创粉丝点击