5

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

2
  • perhaps GridView1.DataSource = myList.ToArray(); would do? Commented Feb 2, 2012 at 12:10
  • Hi Nishit Change Header Text after binding Gridview then it works check the code i posted. Commented Feb 3, 2012 at 11:55

8 Answers 8

3

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});
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

FYI, Datafield="Item" does not work. I ended up using the linq projection which works just great. Could not autogenerate columns because I want to apply an ItemStyle in my Column definition.
1

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>

Comments

0

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>

Comments

0

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

Comments

0

Set Grid AutoGenerateColumns="false"

Comments

0

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+

Comments

-1

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>

1 Comment

method 2 does not work - it seems you can't make BoundField work without a valid "DataField" value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.