views:

60

answers:

3
    protected void Button1_Click(object sender, EventArgs e)
    {

        System.Collections.ArrayList list = new System.Collections.ArrayList();
        list.Add("abc");
        list.Add("xyz");
        list.Add("pqr");
        list.Add("efg");
        GridView1.DataSource = list;
        GridView1.DataBind();
    }

Now when data is bound to the gridview the column name is by default "Items" but I want to change the header text of this column. How to do this..?

A: 

Since you are using auto generated columns, check the Fields collection. Access the first field (Fields[0]) and change the HeaderText to the new value.

Brian
I tried this but this is not working.
Akshay
A: 

Set the HeaderText property of the BoundField, like it's done here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.aspx

citronas
A: 

I was able to get the GridView to bind properly and display a column heading of My Header by doing this:

.aspx

  <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
    <Columns>
        <asp:BoundField HeaderText="My Header" DataField="Value" />
    </Columns>
  </asp:GridView>

.aspx.cs

System.Collections.ArrayList list = new System.Collections.ArrayList();
list.Add(new ListItem("abc"));
list.Add(new ListItem("xyz"));
list.Add(new ListItem("pqr"));
list.Add(new ListItem("efg"));
GridView1.DataSource = list;
GridView1.DataBind();
CAbbott
How does that differ from my solution i posted 4 1/2 hours earlier?
citronas
If you look, I modified the OPs code to surround the data with `ListItems` so that he could actually have a `DataField` to bind to. You only posted a link to the `BoundField` doc, but just adding that to his grid would not have worked by itself.
CAbbott