开发者

How to add a class to a GridView using attributes

i want to add items to a gridview in asp.net from a custom class. The class has Properties X and Y. Does anyone know if im able to add special attributes to these properties so i can just add the class and not have to muck around?

eg..

[Column("Name")]
public string Name { get; set; }

Ideally i can开发者_开发技巧 then write something like..

this.gridview.datasource = instanceOfMyClass;


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜