Associate a non-property with a DataField data column?

Let's say I have a DataGrid that looks something like this:

<asp:DataGrid ID="SomeDataGrid" runat="server"> <Columns> <asp:BoundColumn HeaderText="A Header" SortExpression="Sort" DataField="Data"></asp:BoundColumn> </Columns> </asp:DataGrid> 

In this grid, I set the data source to some collection that contains the public property "Data" and makes databind() . Everyone works as expected.

Now let me say that I want to set the DataField attribute of a column to an open member or property or some other thing that I calculated. What is the easiest way to do this without creating intermediate objects or adding public properties to objects in the collection?

So what I want to do is something like:

 <asp:BoundColumn HeaderText="A Header" SortExpression="Sort" DataField="someMethod()"></asp:BoundColumn> 
+4
source share
1 answer

Use TemplateColumn

 <asp:TemplateColumn HeaderText="Test"> <ItemTemplate> <asp:Label runat="server" Text='<%# SomeMethod() %>'></asp:Label> </ItemTemplate> </asp:TemplateColumn> 
+3
source

Source: https://habr.com/ru/post/1346112/


All Articles