Binding GridView with List of Strings

岁酱吖の 提交于 2019-12-22 06:56:58

问题


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>

And in code behind:

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

:

:

// code to populate myList

:

:

GridView1.DataSource = myList;

GridView1.DataBind();

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.

Thanks


回答1:


Or you can do it like this:

Aspx:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="test" HeaderText="Text" />
    </Columns>
</asp:GridView>

Code:

var ls=new List<string>();
ls.Add("Test");
ls.Add("Test2");
gv.DataSource=ls.Select (l =>new{test=l});



回答2:


Add AutoGenerateColumns="false" to disable the second column; I'm not sure how you would bind a string array; since it outputs Item, maybe add DataField="Item" to your grid definition. Or, bind to an anonymous object:

this.gvw.DataSource = mylist.Select(i => new { Data = i });

And then in your bound column, specify Data as the text field.

Option 3 is to leave the AutoGenerateColumns="true" (the default) and remove your column.




回答3:


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>



回答4:


You can do this programmatically in the RowDataBound event.

protected void GridView_MyList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
              ((Label)e.Row.Cells[1].Controls[0]).Text = "My List";
            }
        }

And your GridView control will be like

<asp:GridView ID="GridView_MyList" runat="server" 
Width="800px" OnRowDataBound="GridView_MyList_RowDataBound"></asp:GridView>



回答5:


Try this:

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>


 List<string> l = new List<string>();
            l.Add("computer");
            l.Add("laptop");
            l.Add("palmtop");

            GridView1.DataSource = l;
            GridView1.DataBind();
            GridView1.HeaderRow.Cells[0].Text = "My List";



回答6:


Set Grid AutoGenerateColumns="false"




回答7:


just use Container.DataItem

.cs

List<string> myList = new List<string>();
GridView1.DataSource = myList ;
GridView1.DataBind();

.aspx

    <asp:gridview ID="GridView1" runat="server">
        <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="nasdc" runat="server" Text=<%# Container.DataItem %>></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>

http://forums.asp.net/t/1050997.aspx?How+to+bind+a+GridView+to+a+List+string+




回答8:


method 1:

make auto generate columns property true

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

</asp:GridView>

method 2:

make auto generate columns property false

<asp:GridView ID="GridView1" runat="server" Width="95%">
    <Columns>
    <asp:BoundField HeaderText="My List" />
    </Columns>
</asp:GridView>


来源:https://stackoverflow.com/questions/9111982/binding-gridview-with-list-of-strings

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