Suppose you have a DataObject
class (equals to MyClass
in your question)
public class DataObject
{
public int ID { get; set; }
public string Name { get; set; }
}
The DataSource of the gridview is not an instance of DataObject
but a List<DataObject>
(or something equivalent), each DataObject
refers to one row in the grid view. On the other hand, it's not a good idea to use attributes marked in DataObject
class. Specifying the DataField
in the columns of the grid view is the easiest way. Here is an example:
<asp:GridView ID="myGridView" runat="server">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
And in code behind:
List<DataObject> data = GetTheData();
myGridView.DataSource = data;