在 DataGridView 中将 AutoSizeMode 设置为 AllCells 时出现 NullReferenceException
NullReferenceException when setting AutoSizeMode to AllCells in DataGridView
我首先将 entity framework 代码 table 手动绑定到数据网格视图。当我将 AutoSizeMode 设置为 AllCells 并将实例添加到 table 时,我在添加过程中得到 NullReferenceException。
代码运行如下:
dbContext.Persons.Load();
myDataGridView.DataSource = dbContext.Persons.Local.ToBindingList();
myDataGridView.Columns[ "Description" ].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
Person p = new Person();
p.Name = "Tester Alfred";
p.Description = "Description"; //no more properties, only those two (Id Property is annotated as [Key]
dbContext.Persons.Add( p ); // this throws a NullReferenceException
这是堆栈跟踪中的相关部分:
System.Data.Entity.Core.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName)
bei System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
bei System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClassd.<Add>b__c()
bei System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
bei System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
bei System.Data.Entity.DbSet`1.Add(TEntity entity)
table 人是空的。当我删除 AutoSize - Instruction 时一切正常。
平台:使用 Studio 2013 的 .Net 4.5.1 中的 WInForms; 运行 Win8 专业版,EF 6.1.3
编辑:删除了引入第二个网格视图的拼写错误
尝试先添加数据再使用
myDataGridView .Columns[ "Description" ] .AutoSizeMode =
DataGridViewAutoSizeColumnMode .AllCells ;
你也可以试试这个
DataGridViewColumn column = dataGridView.Columns[Index];
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
AutoSize all cells 意味着数据网格视图需要评估每一列的结果。 EF 需要为每一行提供结果。看来您可能绑定到外键列。如果行中的值与外部 table 中的行不匹配,则 datagridview 将抛出此错误。 EF 有一个问题,即 table 中的值必须与外部 table 中的值匹配,区分大小写......无论 SQL 是否区分大小写。 EF 正在使用 CLR 执行实体匹配...区分大小写
我首先将 entity framework 代码 table 手动绑定到数据网格视图。当我将 AutoSizeMode 设置为 AllCells 并将实例添加到 table 时,我在添加过程中得到 NullReferenceException。
代码运行如下:
dbContext.Persons.Load();
myDataGridView.DataSource = dbContext.Persons.Local.ToBindingList();
myDataGridView.Columns[ "Description" ].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
Person p = new Person();
p.Name = "Tester Alfred";
p.Description = "Description"; //no more properties, only those two (Id Property is annotated as [Key]
dbContext.Persons.Add( p ); // this throws a NullReferenceException
这是堆栈跟踪中的相关部分:
System.Data.Entity.Core.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName)
bei System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
bei System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClassd.<Add>b__c()
bei System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
bei System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
bei System.Data.Entity.DbSet`1.Add(TEntity entity)
table 人是空的。当我删除 AutoSize - Instruction 时一切正常。
平台:使用 Studio 2013 的 .Net 4.5.1 中的 WInForms; 运行 Win8 专业版,EF 6.1.3
编辑:删除了引入第二个网格视图的拼写错误
尝试先添加数据再使用
myDataGridView .Columns[ "Description" ] .AutoSizeMode =
DataGridViewAutoSizeColumnMode .AllCells ;
你也可以试试这个
DataGridViewColumn column = dataGridView.Columns[Index];
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
AutoSize all cells 意味着数据网格视图需要评估每一列的结果。 EF 需要为每一行提供结果。看来您可能绑定到外键列。如果行中的值与外部 table 中的行不匹配,则 datagridview 将抛出此错误。 EF 有一个问题,即 table 中的值必须与外部 table 中的值匹配,区分大小写......无论 SQL 是否区分大小写。 EF 正在使用 CLR 执行实体匹配...区分大小写