奇怪的行为datagridview [英] Strange behavior datagridview
问题描述
今天我遇到了datagridview的一个奇怪的行为。
当我试图将POCO模型集合附加到gridview时。
POCO课程如下:
class SchemaTaxClass
{
internal 简短 ID { get ; set ; }
内部 简短 TAXCATEGORY_ID { get 跨度>; set ; }
内部 字符串名称{ get 跨度>; set ; }
内部 字符串 ACRONYM { get 跨度>; set ; }
internal System.DateTime EFFECTIVEFROM { get ; set ; }
内部 字符串说明{ get 跨度>; set ; }
internal System.DateTime CREATEDON { get ; set ; }
内部 bool ISACTIVE { get 跨度>; set ; }
}
所有房产均为内部。
在大班我有:
public partial class TaxationUtils:Form
{
List< Taxation.PrivateData.Schemas.SchemaTaxClass< _taxClassesCollection;
Taxation.PrivateData.TaxClassCollection _clsTaxClassCollection;
public TaxationUtils()
{
InitializeComponent();
SynchronizeTaxClassCollection();
}
void SynchronizeTaxClassCollection()
{
try
{
GetOrRefreshTaxClassCollection( null );
PopulateGridWithExistingTaxClasses();
}
catch (Exception ex)
{
AppendMsg(ex.ToString());
}
}
void GetOrRefreshTaxClassCollection( short ?taxCategoryID)
{
尝试
{
if (_ clsTaxClassCollection = = null )
_clsTaxClassCollection = new PrivateData.TaxClassCollection(taxCategoryID);
_taxClassesCollection = _clsTaxClassCollection.GetTaxClassCollection;
}
catch (例外情况)
{
throw ex;
}
}
void PopulateGridWithExistingTaxClasses()
{
dgv_ExistingTaxClasses.DataSource = _taxClassesCollection.ToList() ;
}
}
这是我们获取的数据类数据(通过实体框架)
class TaxClassCollection
{
短跨度>? _taxCategoryID = null ;
bool FLAG_ERR;
列表< SchemaTaxClass> _taxClassList = null ;
internal TaxClassCollection( short ?taxCategoryID)
{
this ._ taxCategoryID = taxCategoryID;
}
内部列表< SchemaTaxClass> GetTaxClassCollection
{
get
{
FLAG_ERR = false ;
尝试
{
GetTaxClasses();
if (!FLAG_ERR&& _taxClassList!= null )
< span class =code-keyword> return _taxClassList;
else
return null < /跨度>;
}
catch (例外情况)
{
throw ex;
}
}
私有 设置
{
_taxClassList =(List< SchemaTaxClass>) value ;
}
}
void GetTaxClasses()
{
_taxClassList = new 列表< SchemaTaxClass>();
尝试
{
使用( var _context = new vk1_0Entities())
{
_taxClassList =(来自 taxClass _context.TAX_CLASS
其中 taxClass.taxZone_id == BusinessInfo.TaxZoneID
}
if (FLAG_ERR)
FLAG_ERR = false ;
}
catch (例外情况)
{
if (!FLAG_ERR)
FLAG_ERR = true ;
throw ex;
}
}
}
发生的事情是什么时候发生的POCO类成员具有内部修饰符,gridview不显示数据。
如果修饰符变为公共,则数据可见。
为什么会出现这样的行为?
备注:
POCO类,数据类和主类都在同一个程序集和命名空间中。 />
此外,数据存在于用作gridview数据源的集合变量中。
我尝试过:
我试过从MSDN探索网格视图,但没有发现任何可以证明这种行为的理由。
当一切都在相同的程序集和命名空间,内部必须工作。
这不是奇怪的行为 - 据我所知,它是设计的,绝对是我的期望。
将DataSource绑定到集合时,表模式是constr通过反射检查集合对象数据类型的属性来检测 - 这意味着只有public
属性可以显示为执行绑定的类或程序集可能无法访问非public
属性,因此不应显示它们。否则会破坏设计中的保护!
它不会显示任何私人
,内部
,protected
或protected internal
属性 - 仅public
I came across a strange behavior by datagridview today.
It was when I was trying to attach a POCO model collection to gridview.
The POCO class is as:
class SchemaTaxClass
{
internal short ID { get; set; }
internal short TAXCATEGORY_ID { get; set; }
internal string NAME { get; set; }
internal string ACRONYM { get; set; }
internal System.DateTime EFFECTIVEFROM { get; set; }
internal string DESCRIPTION { get; set; }
internal System.DateTime CREATEDON { get; set; }
internal bool ISACTIVE { get; set; }
}
All properties are INTERNAL.
In main class I have:
public partial class TaxationUtils : Form
{
List<Taxation.PrivateData.Schemas.SchemaTaxClass< _taxClassesCollection;
Taxation.PrivateData.TaxClassCollection _clsTaxClassCollection;
public TaxationUtils()
{
InitializeComponent();
SynchronizeTaxClassCollection();
}
void SynchronizeTaxClassCollection()
{
try
{
GetOrRefreshTaxClassCollection(null);
PopulateGridWithExistingTaxClasses();
}
catch (Exception ex)
{
AppendMsg(ex.ToString());
}
}
void GetOrRefreshTaxClassCollection(short? taxCategoryID)
{
try
{
if (_clsTaxClassCollection == null)
_clsTaxClassCollection = new PrivateData.TaxClassCollection(taxCategoryID);
_taxClassesCollection = _clsTaxClassCollection.GetTaxClassCollection;
}
catch (Exception ex)
{
throw ex;
}
}
void PopulateGridWithExistingTaxClasses()
{
dgv_ExistingTaxClasses.DataSource = _taxClassesCollection.ToList();
}
}
This is the Data Class from where we fetch data (through Entity Framework)
class TaxClassCollection
{
short? _taxCategoryID = null;
bool FLAG_ERR;
List<SchemaTaxClass> _taxClassList = null;
internal TaxClassCollection(short? taxCategoryID)
{
this._taxCategoryID = taxCategoryID;
}
internal List<SchemaTaxClass> GetTaxClassCollection
{
get
{
FLAG_ERR = false;
try
{
GetTaxClasses();
if (!FLAG_ERR && _taxClassList != null)
return _taxClassList;
else
return null;
}
catch (Exception ex)
{
throw ex;
}
}
private set
{
_taxClassList = (List<SchemaTaxClass>)value;
}
}
void GetTaxClasses()
{
_taxClassList = new List<SchemaTaxClass>();
try
{
using (var _context = new vk1_0Entities())
{
_taxClassList = (from taxClass in _context.TAX_CLASS
where taxClass.taxZone_id == BusinessInfo.TaxZoneID
}
if (FLAG_ERR)
FLAG_ERR = false;
}
catch (Exception ex)
{
if (!FLAG_ERR)
FLAG_ERR = true;
throw ex;
}
}
}
What's happening is when POCO class members have internal as modifier, gridview doesn't display the data.
If modifier is turned to public, data is visible.
Why is such behaviour?
Remarks:
POCO class, data class and main class are all in same assembly and namespace.
Also, data is present in collection variable used as datasource for gridview.
What I have tried:
I tried exploring grid view from MSDN but found nothing that can justify this behavior.
When everything is in same assembly and namespace, internal must work.
This is not "strange" behaviour - as far as I can see it's by design, and absolutely what I'd expect.
When you bind a DataSource to a collection, the table schema is constructed by examining the properties of the collection object datatype via reflection - which means that onlypublic
properties can be displayed as the class or assembly doing the binding may not have access to non-public
properties, so they should not be displayed. To do otherwise would be breaking the protection designed into the system!
It will not display anyprivate
,internal
,protected
, orprotected internal
properties - onlypublic
.
这篇关于奇怪的行为datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!