.Net 2.0 C# GridView question

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
 
P

Prashanth Rao

Jason,

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

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

Regards,
Prashanth.
 
G

Guest

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
 
P

Prashanth Rao

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top