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.