Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding GridView with List of Strings

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

like image 586
Nishit Jain Avatar asked Oct 08 '22 20:10

Nishit Jain


1 Answers

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});
like image 109
Arion Avatar answered Oct 13 '22 12:10

Arion