Click here to Skip to main content
16,020,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Let me explain the implementation first. Below is the base classes that I am using

C#
public class DataClass
   {
       public string name;
       public int member;
   }



Following will give the data to the GridView
XML
public class DataFields
   {
       public static List<DataClass> getData()
       {
           List<DataClass> dataList = new List<DataClass>();
           for (int i = 1; i < 10; i++)
           {
               DataClass data = new DataClass();
               data.name = "H" + i;
               data.member = i;
               dataList.Add(data);
           }
           return dataList;
       }
   }



Following is the aspx page class
C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               GridView1.DataSource = DataFields.getData();
               GridView1.DataBind();
           }
       }


Though this is a simple implementation I got "gridview did not have any properties or attributes" error. But I solved this by implementing the DataClass in the following way

C#
public class DataClass
  {
      public string name { get; set; }
      public int member { get; set; }
  }


I want to know why there is no error this time as I m using both the variables as public even before. I know it is not necessary to use the getter and setter method in all case. But I want to know why do we have to use under the above case.
Posted
Updated 9-Jan-14 23:30pm
v2
Comments
Sandeep Singh Shekhawat 10-Jan-14 5:39am    
Because GridView checks Column name as a properties of object which list binds to grid view as Data source . AS pre my understanding you have auto increment column for grid view instead of manually create any column for grid view.

Because the data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns. To generate columns to gridview you need to properties in data source.

DataBind() method of GridView always checks property in data source to bind grid view and creates columns.
 
Share this answer
 
v2
Comments
Prasaad SJ 12-Jan-14 22:16pm    
Hi.. Properties will be name and member even if with or without getter /setter method but then Grid View is working in one case and not the other..
If you are using Custom Types like T ( ur example: DataClass ), You should Properties only , not the fields
 
Share this answer
 

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