Nishit Jain
Nishit Jain

Reputation: 1579

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

Upvotes: 5

Views: 16886

Answers (8)

Mike
Mike

Reputation: 550

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>

Upvotes: 1

Abdul Saboor
Abdul Saboor

Reputation: 4127

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+

Upvotes: 0

Emad Mokhtar
Emad Mokhtar

Reputation: 3297

Set Grid AutoGenerateColumns="false"

Upvotes: 0

Vinod
Vinod

Reputation: 4872

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

Upvotes: 0

Madhu Beela
Madhu Beela

Reputation: 2215

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>

Upvotes: -1

Bastardo
Bastardo

Reputation: 4152

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>

Upvotes: 0

Arion
Arion

Reputation: 31239

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});

Upvotes: 3

Brian Mains
Brian Mains

Reputation: 50728

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.

Upvotes: 1

Related Questions