问题描述
用的EF6+codefirst,自己定义了一个entity:namespaceDAL_EF.Objects{publicclassInstitution{#region必填信息[Key]publicstringInsID{get;privateset;}publicstringInsName{get;privateset;}publicstringSupervisor{get;privateset;}publicstringInsAddress{get;privateset;}……publicvirtualICollection<Contactor>Contactors{get;privateset;}publicInstitution(){}}}
其中Contactor定义如下:namespaceDAL_EF.Objects{publicclassContactor{[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]publicGuidID{get;privateset;}publicstringName{get;privateset;}publicInstitutionWorkIn{get;privateset;}publicstringDepart{get;privateset;}publicstringDuty{get;privateset;}publicContactor(){}}}
在frame中有一个datalayoutcontrol,用来展示该实体,使用textedit绑定属性:this.InsIDTextEdit.DataBindings.Add(newSystem.Windows.Forms.Binding("EditValue",this.institutionBindingSource,"InsID",true));
其中institutionBindingSource:this.institutionBindingSource.DataSource=typeof(DAL_EF.Objects.Institution);
对于Contactors,使用gridcontrol:this.gridControl1.DataMember="Contactors";this.gridControl1.DataSource=this.institutionBindingSource;
frame中有一个方法,可以弹出一个dialog新增或者修改Contactor。最后问题来了……我新增或修改一个Contactor后,如何能让gridControl1自己更新新增或更新数据呢??求大神指点啊!用institutionBindingSource.Restbinding不起作用……打断点,发现无论是institutionBindingSource的datasource还是gridControl1的datasource里面的数据其实是已经更新了的,只是gridControl1的显示上没有变化,只能重新打开frame才显示正确,十分无奈。。。。。网上找了一大圈,老外有用currentManager的refrsh方法来弄的。但是我这报错:代码如下://gridControl1.MainView.BeginDataUpdate();//gridControl1.RefreshDataSource();//gridView1.RefreshData();CurrencyManagercm=(CurrencyManager)this.BindingContext[gridControl1.DataSource];cm.Refresh();//gridControl1.ResetBindings();//institutionBindingSource.ResetBindings(false);//gridControl1.MainView.EndDataUpdate();
里面注释掉的方法都是尝试了不起作用的。。。。。用CM报错:引用
未处理System.InvalidCastExceptionHResult=-2147467262Message=无法将类型为“System.Collections.Generic.HashSet`1[DAL_EF.Objects.Contactor]”的对象强制转换为类型“DAL_EF.Objects.Contactor”。Source=DAL_EFStackTrace:在(Object)在DevExpress.Data.Access.DataListDescriptor.FastPropertyDescriptor.GetValue(Objectcomponent)在DevExpress.Data.Helpers.BaseListDataControllerHelper.GetRowValue(Int32listSourceRow,Int32column,OperationCompletedcompleted)在DevExpress.Data.DataController.GetRowValue(Int32controllerRow,Int32column,OperationCompletedcompleted)在DevExpress.Data.BaseListSourceDataController.GetRowValue(Int32controllerRow,Int32column,OperationCompletedcompleted)在DevExpress.XtraGrid.Views.Grid.GridView.GetRowCellValue(Int32rowHandle,GridColumncolumn,OperationCompletedcompleted)在DevExpress.XtraGrid.Views.Grid.GridView.GetRowCellValue(Int32rowHandle,GridColumncolumn)在DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.GetRowCellValue(GridCellInfocell,Int32rowHandle,GridColumnInfoArgsci)在DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.CalcRowCellDrawInfo(GridDataRowInfori,GridColumnInfoArgsci,GridCellInfocell,GridColumnInfoArgsnextColumn,BooleancalcEditInfo,GridRownextRow)在DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.CalcRowCellsDrawInfo(GridDataRowInfori,GridColumnsInfocolumnsInfo,GridRownextRow)在DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.CalcDataRow(GridDataRowInfori,GridRowrow,GridRownextRow)在DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.CalcRowsDrawInfo()在DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.CalcGridInfo()在DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.Calc(Graphicsg,Rectanglebounds)在DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.FullRecalcScroll(Graphicsg,Rectanglebounds)在DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.CalcAfterVertScrollCore(Graphicsg,Rectanglebounds,BooleanuseCache)在DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.CalcAfterVertScroll(Graphicsg,Rectanglebounds,BooleanuseCache)在DevExpress.XtraGrid.Views.Grid.GridView.RefreshRows(BooleanuseCache,BooleanafterScroll)在DevExpress.XtraGrid.Views.Grid.GridView.CalculateDataCore()在DevExpress.XtraGrid.Views.Grid.GridView.CalculateData()在DevExpress.XtraGrid.Views.Base.BaseView.CheckViewInfo()在DevExpress.XtraGrid.Views.Base.BaseView.Draw(GraphicsCachee)在DevExpress.XtraGrid.GridControl.OnPaint(PaintEventArgse)在DevExpress.Utils.Drawing.ControlPaintHelper.PaintWithErrorHandling(PaintEventArgspaintEventArgs,Int32layer)在DevExpress.Utils.Drawing.ControlPaintHelper.ProcessWMPaint(Message&m)在DevExpress.XtraGrid.GridControl.CheckProcessMsg(Message&m)在DevExpress.XtraGrid.GridControl.WndProc(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtrhWnd,Int32msg,IntPtrwparam,IntPtrlparam)InnerException:
请问个问题出来哪里???该如何解决呢?或者,有没有更简便的方法能够刷新数据。。。?本人纯菜鸟……大神勿喷
解决方案
本帖最后由 gysos 于 2016-06-17 00:24:08 编辑
解决方案二:
补充一下,如果直接给gridcontrol的datasource可以实现自动刷新:this.gridControl1.DataBindings.Add(newSystem.Windows.Forms.Binding("DataSource",this.institutionBindingSource,"Contactors",true));
但是!!一觉醒来,又不行了,,,,,简直搞不懂啊!!!!!
解决方案三:
没有人么??求帮忙求指导!!!!