当从左到右滚动数据网格视图时,Object dispose异常
本文关键字:Object dispose 异常 视图 网格 从左到右 滚动 数据 数据网 | 更新日期: 2023-09-27 18:09:55
我使用Linq到SQL类来管理数据,并使用以下代码将数据绑定到Windows窗体中的dataGridview。当我试图从左向右滚动datagdview时抛出异常。
这里涉及的两个表是:
billing_UserRole
RoleId PK
RoleName
CreatedTS
UpdatedTS
billing_User
UserId PK//在其他一些表中也被引用。
RoleId颗
用户名
密码FirstName
LastName
CreatedTS
UpdatedTS
创建表的查询遵循
CREATE TABLE [dbo].[billing_UserRole](
bk_UserRoleId BIGINT IDENTITY(100,1) NOT NULL,
bk_UserRoleName NVARCHAR(128) NOT NULL,
bk_CreatedTS DATETIME NULL,
bk_UpdatedTS DATETIME NULL,
CONSTRAINT [PK_billingUserRole] PRIMARY KEY CLUSTERED
(
[bk_UserRoleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
/* TABLE NAME : billing_User */
CREATE TABLE [dbo].[billing_User](
bk_UserId BIGINT IDENTITY(1,1) NOT NULL,
bk_Username NVARCHAR(256) NOT NULL,
bk_Password NVARCHAR(2048) NOT NULL DEFAULT('none'),
bk_FirstName NVARCHAR(256) NOT NULL,
bk_LastName NVARCHAR(256) NOT NULL,
bk_MobileNo NVARCHAR(128) NOT NULL,
bk_Address NVARCHAR(2048) NULL DEFAULT('none'),
bk_Remarks NVARCHAR(2048) NULL DEFAULT('none'),
bk_UserRoleId BIGINT NOT NULL,
bk_CreatedTS DATETIME NULL,
bk_UpdatedTS DATETIME NULL,
CONSTRAINT [PK_billingUser] PRIMARY KEY CLUSTERED
(
[bk_UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [FK_billingUser_billingUserRole] FOREIGN KEY(bk_UserRoleId) REFERENCES [dbo].[billing_UserRole](bk_UserRoleId)
)
form中绑定datagridview到form_load
private void UserManagementForm_Load(object sender, EventArgs e)
{
try
{
using (BillingKioskDataLayerDataContext dc = new BillingKioskDataLayerDataContext())
{
Table<billing_User> UserTable = dc.GetTable<billing_User>();
var UserList = UserTable.Select(o=>o).ToList();
billing_UserBindingSource.DataSource = UserList;
dataGridView1.DataSource = billing_UserBindingSource;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
下面是抛出异常的代码。这是一个自动生成的代码。为什么会这样呢?当我滚动DataGridView从左到右查看剩余字段时,我只得到这个异常。
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="billing_UserRole_billing_User", Storage="_billing_UserRole", ThisKey="bk_UserRoleId", OtherKey="bk_UserRoleId", IsForeignKey=true)]
public billing_UserRole billing_UserRole
{
get
{
return this._billing_UserRole.Entity; **// I get DisposedObjectException :Cannot access a disposed object here**
}
set
{
billing_UserRole previousValue = this._billing_UserRole.Entity;
if (((previousValue != value)
|| (this._billing_UserRole.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._billing_UserRole.Entity = null;
previousValue.billing_Users.Remove(this);
}
this._billing_UserRole.Entity = value;
if ((value != null))
{
value.billing_Users.Add(this);
this._bk_UserRoleId = value.bk_UserRoleId;
}
else
{
this._bk_UserRoleId = default(long);
}
this.SendPropertyChanged("billing_UserRole");
}
}
}
堆栈跟踪:
at System.Data.Linq.DataContext.GetTable(Type type)
at System.Data.Linq.CommonDataServices.GetDataMemberQuery(MetaDataMember member, Expression[] keyValues)
at System.Data.Linq.CommonDataServices.DeferredSourceFactory`1.ExecuteKeyQuery(Object[] keyValues)
at System.Data.Linq.CommonDataServices.DeferredSourceFactory`1.Execute(Object instance)
at System.Data.Linq.CommonDataServices.DeferredSourceFactory`1.DeferredSource.GetEnumerator()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Data.Linq.EntityRef`1.get_Entity()
at BillingKiosk.billing_User.get_billing_UserRole() in e:'XGHProjects'BillingKiosk'BillingKiosk'BillingKioskDataLayer.designer.cs:line 663
很可能您正在经历相关实体中延迟或延迟加载列的情况。您可以使用DataLoadOptions。方法来包含相关的列。
http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.loadwith (v = vs.110) . aspx
顺便说一句:很容易建立,如果这是真的情况:删除你的数据上下文的Using()。[仅用于诊断…]。如果工作正常,延迟加载…