Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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:
C#
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)
C#
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.
Posted
Updated 19-Nov-16 23:58pm

1 solution

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 only public 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 any private, internal, protected, or protected internal properties - only public.
 
Share this answer
 
Comments
Member 11040029 20-Nov-16 6:01am    
Yeah it makes sense.
Member 11040029 20-Nov-16 6:05am    
But how about if I do not want to expose a few properties of the POCO class to other projects? Making each property public will provide access to other objects in other projects to spy on properties that must not be given access to them.
OriginalGriff 20-Nov-16 6:19am    
Then you can't use it to populate a Data Source.
The alternative is to provide a limited public class, and a less restrictive internal class that encapsulates the public instance and returns it's internal properties via it's own public properties. Or derives from it, and adds public properties that return the internal values under a new name (because you can't change access modifiers when you override properties or methods)
Member 11040029 20-Nov-16 7:06am    
tnx griff!
OriginalGriff 20-Nov-16 7:10am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900