开发者

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

开发者 https://www.devze.com 2023-02-19 20:08 出处:网络
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 str

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] ) 开发者_StackOverflow中文版%> in the markup?


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


You just need:

   <%# GetDataItem().ToString() %>

See the MSDN documentation for more information.


You should be able to use

<%# Container.DataItem %>

to bind the string items to your GridView.

0

精彩评论

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