List<string> as GridView Datasource. What do I put for DataField in the GV's BoundField?

微笑、不失礼 提交于 2020-01-23 11:02:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!