结合GridView控件与字符串列表 [英] Binding GridView with List of Strings

查看:183
本文介绍了结合GridView控件与字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名单,我绑定到一个GridView,所以我的GridView控件将只有一个,这些字符串值的列。我想有一个适当的标题文本此列。请帮帮我。看看我试图做的事:

I have a List that I am binding to a GridView, therefore my GridView will have only one column of these string values. I want to have a proper header-text for this column. Please help me. See what I have attempted to do:

<asp:GridView ID="GridView1" runat="server" Width="95%">

<Columns>

<asp:BoundField HeaderText="My List" />

</Columns>

</asp:GridView>

和在code背后:

List<string> myList = new List<string>();

:

:

// code to populate myList

:

:

GridView1.DataSource = myList;

GridView1.DataBind();

当我运行这个code,我得到在GridView两列。有头文字为我的目录,并有空白行第一列,而其标题文字为项目,并具有myList中值的行第二列。我想在我有头文字GridView控件为我的名单和行与myList中对象的值,只有一个列。

When I run this code, I get two columns in the GridView. First column having header-text as "My List" and having blank rows, whereas the second column having header-text as "Item" and having rows with myList values. I want to have only one column in my GridView having header-text as "My List" and rows with the values of the myList object.

感谢

推荐答案

我相信下面会给你你正在寻找的结果:

I believe the following will give you the results you are looking for:

        <asp:gridview id="MyGridView" runat="server" showheaderwhenempty="true" autogeneratecolumns="false" runat="server">
        <Columns>
            <asp:TemplateField HeaderText="My List">
            <ItemTemplate>
                <asp:Label ID="Column1" runat="server" Text=<%# Container.DataItem %>></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:gridview>

这篇关于结合GridView控件与字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆