问题
I have a List<string> that I'm using as a DataSource for a GridView. I don't want to auto generate the columns (it gives "Item" as the header). What do I put for the DataField to get the string if I'm using a BoundField? aka <%# Eval( [whatgoeshere] ) %> in the markup?
回答1:
You will have to use TemplateField in that case and not BoundField like:
<asp:TemplateField HeaderText="My Header">
<ItemTemplate>
<%#Container.DataItem %>
</ItemTemplate>
</asp:TemplateField>
But to your root problem of column header being "Item" you can set your Column Header to your desired value in code-behind with AutoGenerateColumns. e.g.
GridView1.DataSource = list;
GridView1.DataBind();
GridView1.HeaderRow.Cells[0].Text = "My Custom Header";
回答2:
You just need:
<%# GetDataItem().ToString() %>
See the MSDN documentation for more information.
回答3:
You should be able to use
<%# Container.DataItem %>
to bind the string items to your GridView.
来源:https://stackoverflow.com/questions/5426454/liststring-as-gridview-datasource-what-do-i-put-for-datafield-in-the-gvs-bou