Click here to Skip to main content
15,885,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
see the example!!!

C#
public class ClsProduct
   {
       public string pName;

       public int pPrice;

   }

           ClsProduct objclsProduct1=new ClsProduct();
           objclsProduct.pName="apple";
           objclsProduct.pPrice=140;
           ClsProduct objclsProduct2=new ClsProduct();
           objclsProduct.pName="orange";
           objclsProduct.pPrice=70;
           List<ClsProduct> lst=new List<clsProduct>();
           lst.add(objclsProduct1);
           lst.add(objclsProduct2);
           GridView1.DataSource = lst;
           GridView1.DataBind();



it wont work !!!!


but...



C#
public class ClsProduct
    {
        public string pName
        {
          get;set; 
        }

        public int pPrice
        {
         get;set;
        }

    }

            ClsProduct objclsProduct1=new ClsProduct();
            objclsProduct.pName="apple";
            objclsProduct.pPrice=140;
            ClsProduct objclsProduct2=new ClsProduct();
            objclsProduct.pName="orange";
            objclsProduct.pPrice=70;

            List<ClsProduct> lst=new List<clsProduct>();
            lst.add(objclsProduct1);
            lst.add(objclsProduct2);
            GridView1.DataSource = lst();
            GridView1.DataBind();


this will work!!!

that means gridview will bind with properties but not with public variables...
Why ???
Posted
Updated 22-May-12 22:23pm
v3

Databinding could be done only with dependency properties! Please refer the following materials..

http://msdn.microsoft.com/en-us/library/ms752914.aspx

http://msdn.microsoft.com/en-us/library/ms752347.aspx


http://wpftutorial.net/DataBindingOverview.html
 
Share this answer
 
Comments
samu4u 26-May-12 3:13am    
5
Maciej Los 31-May-12 2:27am    
Useful links, +5!
Jim Jos 31-May-12 2:29am    
Thank you losmac!
Because Properties and fields are very different things: fields are directly accessible variables, properties are accessed via a method call (or two in reality - one for get and one for set). Properties are accessed via Reflection when the data binding takes place, variables aren't.

There are other reasons, involving the INotifyPropertyChanged interface, but it gets kinda complicated so you would be best reading a good, think book on databinding! Unless you really need to know, just stick to the idea that Properties are exposed to databinding, and fields aren't.

You shouldn't be exposing Fields as public anyway! :D
 
Share this answer
 
Comments
VJ Reddy 23-May-12 4:36am    
Nice answer. 5!
Espen Harlinn 23-May-12 5:23am    
5'ed!
Maciej Los 31-May-12 2:25am    
Good answer, my 5!
This is because 'You can't databind against a variable.' This is one of the reasons why using Properties is helpful.

Read about properties here:
MSDN: Properties (C# Programming Guide)[^]
Why Properties Matter[^]

This is an interesting blog post: Properties vs. Public Variables[^]
 
Share this answer
 
Comments
VJ Reddy 23-May-12 4:36am    
Nice answer. 5!
Sandeep Mewara 23-May-12 14:26pm    
Thanks VJ.
Espen Harlinn 23-May-12 5:23am    
5'ed!
Sandeep Mewara 23-May-12 14:26pm    
Thanks Espen.
Maciej Los 31-May-12 2:24am    
Good answer, my 5!
The Data Binding concepts are explained here
http://support.microsoft.com/kb/307860[^]
http://msdn.microsoft.com/en-us/library/ms752347(v=vs.90).aspx[^]

Binding sources are explained here
http://msdn.microsoft.com/en-us/library/ms743643.aspx[^]

At the above reference it is stated that You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object.

and in the same reference it is stated that
You cannot bind to public fields.

Hence, due to the design philosophy of .NET data binding it is not possible to directly bind to the public fields.

Further as per the .NET frame work design guidelines it is stated Do not provide instance fields that are public or protected. as explained here http://msdn.microsoft.com/en-us/library/ms229057.aspx[^]
 
Share this answer
 
Comments
Espen Harlinn 23-May-12 5:24am    
5'ed!
VJ Reddy 23-May-12 6:19am    
Thank you, Espen :)
Mohammad A Rahman 23-May-12 6:24am    
my 5!
VJ Reddy 23-May-12 6:26am    
Thank you, Rahman :)
Mohamed Mitwalli 23-May-12 9:39am    
5+

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