.Net 2.0 C# GridView question

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

Would someone tell me how to manipulate the Header of a GridView?
My DataSet is like this:

_id _name
1 John
2 Mary

But I want the GridView in my webform to show up like this:

ID Name
1 John
2 Mary

Thanks for help.


Jason
 
Jason,

You can change the header text of the GridView column by:

GridView1.Columns[0].HeaderText = "ID";

Regards,
Prashanth.
 
Another way is to define the column your self that way you know for shaw the
heading corresponds to the correct data see below:

//disable auto column generation
MyGridView.AutoGenerateColumns = false;

//define id column
BoundField field = new BoundField();
field.DataField = "Id";
field.HeaderText = "TestId";
MyGridView.Columns.Add(field);

//define Name column
field.DataField = "Name";
field.HeaderText = "TestName";
MyGridView.Columns.Add(field);

MyGridView.DataSource = _myDataSource;
MyGridView.DataBind();

Prashanth Rao said:
Jason,

You can change the header text of the GridView column by:

GridView1.Columns[0].HeaderText = "ID";

Regards,
Prashanth.


Hi,

Would someone tell me how to manipulate the Header of a GridView?
My DataSet is like this:

_id _name
1 John
2 Mary

But I want the GridView in my webform to show up like this:

ID Name
1 John
2 Mary

Thanks for help.

Jason
 
One more way is to set it in the Design time like this:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="First Name"
SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("Id") %>' Width="108px"></asp:Label>
</ItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#
Bind("Name") %>' Width="108px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</gridview>
 
Back
Top