已经处理了ObjectContext实例,并且不再使用错误发生

我有一个Windows窗体应用程序 . 我有一个基本窗口,我正在尝试打开一个带有datagridview的查找窗口 . 我正在打开一个自定义的表单控件,如下面的代码所示 . 然后将选定的值读取为另一种形式的变量 .

if (e.KeyCode == Keys.F3)
                {
                    using (DataControllers.RIT_Allocation_Entities RAEntity = new DataControllers.RIT_Allocation_Entities())
                    {
                        lookupGridSourceofJheader = RAEntity.JOB_Header.ToList<DataControllers.JOB_Header>();
                    }

                    var btnOk = new Button() { Text = "Ok", Anchor = AnchorStyles.None };
                    var btnCancel = new Button() { Text = "Cancel", Anchor = AnchorStyles.Right };

                    var dg = new DataGridView();
                    var bs = new BindingSource();

                    bs.DataSource = lookupGridSourceofJheader;
                    dg.DataSource = bs;
                    dg.Dock = DockStyle.Fill;
                    dg.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

                    //setup a layout which will nicely fit to the window
                    var layout = new TableLayoutPanel();
                    layout.Controls.Add(dg, 0, 0);
                    layout.SetColumnSpan(dg, 2);
                    layout.Controls.Add(btnCancel, 1, 1);
                    layout.Controls.Add(btnOk, 0, 1);
                    layout.RowStyles.Add(new RowStyle(SizeType.Percent));
                    layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent));
                    layout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                    layout.Dock = DockStyle.Fill;

                    //create a new window and add the cotnrols
                    var window = new Form();
                    window.StartPosition = FormStartPosition.CenterScreen;
                    window.Controls.Add(layout);

                    // set the ok and cancel buttons of the window
                    window.AcceptButton = btnOk;
                    window.CancelButton = btnCancel;
                    btnOk.Click += (s, ev) => { window.DialogResult = DialogResult.OK; };
                    btnCancel.Click += (s, ev) => { window.DialogResult = DialogResult.Cancel; };

                    if (window.ShowDialog() == DialogResult.OK)
                    {
                        selectedJobheader = (DataControllers.JOB_Header)bs.Current;
                        txtJID.Text = selectedJobheader.JobID.ToString();
                        txtCustomer.Text = selectedJobheader.CustomerCode.ToString();
                        txtCustomerName.Text = selectedJobheader.CustomerName.ToString();
                        txtRemarks.Text = selectedJobheader.Remarks.ToString();
                        if (selectedJobheader.Status)
                        {
                            txtStatus.Text = "Pending";
                        }
                        else
                        {
                            txtStatus.Text = "Done";
                        }

                        dtpDate.Value = selectedJobheader.JobDate;
                        lblCustomerTelephone.Text = selectedJobheader.MobileNo.ToString();
                        lblCusLocation.Text = selectedJobheader.LocationCode.ToString();
                        populateJdetailsDatagrid();

                    }
                }

当我尝试滚动Datagrid后发生错误 .

--------------------------- DataGridView默认错误对话框------------------ --------- DataGridView中发生以下异常:System.Reflection.TargetInvocationException:对象'System.Data.Entity.DynamicProxies.TBLM_PRODUCT_5D1C9C0350626BB4D225E83C26CEC12F74C76AE392AE2AD916BEFD258BCBEF2A'上的属性访问者'Job_Details'引发了以下异常:'ObjectContext实例已被处置,不能再用于需要连接的操作 . ---> System.ObjectDisposedException:ObjectContext实例已被释放,不能再用于需要连接的操作 . 在System.Data.Entity.Core.Objects.Object.Empity.Core.Object.Object.Etity.Core.Object.Affity . (MergeOption mergeOption)在System.Data.Entity.Core.Objects.DataClasses.EntityCollection1.Load(List1集合,MergeOption mergeOption)的System.Data.Entity.Core.Objects.DataClasses.EntityCollection1.Load(MergeOption mergeOption)at at在System.Data.Entity.Core.Object.Inter.LazyLoadBehavior中的System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad()处的System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.Load() . 在System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior . <> c__DisplayClass72.b__1(TProxy代理,TItem项目)处的LoadProperty [TItem](TItem propertyValue,String relationshipName,String targetRoleName,Boolean mustBeNull,Object wrapperObject)at System.Data.Entity.DynamicProxies.TBLM_PRODUCT_5D1C9C0350626BB4D225E83C26CEC12F74C76AE392AE2AD916BEFD258BCBEF2A . get_Job_Details()---内部异常堆栈跟踪的结束---在System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetValue(Int32 boundColumnIndex,Int32 columnIndex,Int32 rowIndex)的System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component)处替换此默认对话框请处理DataError事件 . - - - - - - - - - - - - - - 好

回答(0)