To set Column Width for Gridview

G

Guest

The following code is copied from a GridView control.
Both the header text and the date are displayed in two rows.
What is the best way to display the whole data in a single row?

Should we specify width? if so, what is the syntax?

Thanks.


<asp:templatefield HeaderText="Created On" SortExpression="dte_cr_on">
<itemtemplate>
<asp:Label ID="gLabel_dte_cr_on" runat="server" Text='<%#
Bind("dte_cr_on") %>'> </asp:Label>

</itemtemplate>
<edititemtemplate>
<asp:Label ID="gLabel_dte_cr_on1" runat="server" Text='<%#
Bind("dte_cr_on") %>'> </asp:Label>
</edititemtemplate>
</asp:templatefield>
 
S

Steven Cheng[MSFT]

Hi Rkbnair,

If you want to set column witdth for GridView, you can use the
<HeaderStyle> or <ItemStyle> to set witdth for template column. e.g.

=====================
<asp:TemplateField HeaderText="used" SortExpression="used">
<HeaderStyle Width="100" />
<ItemStyle Width="100" />

<HeaderTemplate>
fjsfjkldsfksdjfdsfjfkksfsdkfdsjfjkfdsj
</HeaderTemplate>

<EditItemTemplate>
..................
======================

BTW, is there anything else(such as a container html element in your page)
that cause the gridview column width be restricted? I've tried put long
text in <headerTemplate> without setting width and it doesn't break into
mutlti lines. e.g.

============
<asp:TemplateField............>
<HeaderTemplate>
fjsfjkldsfksdjfdsfjfkksfsdkfdsjfjkfdsj
</HeaderTemplate>
..........
=============

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
G

Guest

The problem with this syntax is that I cannot make the column sortable by
clicking on the header.

What is the difference if I specify width in the following way:

<asp:templatefield
HeaderText="Complex ID"
SortExpression="str_complex_id"><itemtemplate><asp:Label
ID="gLabel_str_complex_id" Width="200px" runat="server"
Text='<%# Bind("str_complex_id") %>'> </asp:Label></itemtemplate>
<edititemtemplate><asp:TextBox ID="gTextBox_str_complex_id" Width="200px"
runat="server"
MaxLength="16"
Text='<%# Bind("str_complex_id")%>' OnChange="upperCase(this.id)">
</asp:TextBox>
</edititemtemplate>
<footertemplate><asp:TextBox ID="gfTextBox_str_complex_id" Width="200px"
runat="server"
MaxLength="16"
Text='<%# Bind("str_complex_id")%>' OnChange="upperCase(this.id)">
</asp:TextBox>
</footertemplate>
</asp:templatefield>

--
test


Steven Cheng said:
Hi Rkbnair,

If you want to set column witdth for GridView, you can use the
<HeaderStyle> or <ItemStyle> to set witdth for template column. e.g.

=====================
<asp:TemplateField HeaderText="used" SortExpression="used">
<HeaderStyle Width="100" />
<ItemStyle Width="100" />

<HeaderTemplate>
fjsfjkldsfksdjfdsfjfkksfsdkfdsjfjkfdsj
</HeaderTemplate>

<EditItemTemplate>
..................
======================

BTW, is there anything else(such as a container html element in your page)
that cause the gridview column width be restricted? I've tried put long
text in <headerTemplate> without setting width and it doesn't break into
mutlti lines. e.g.

============
<asp:TemplateField............>
<HeaderTemplate>
fjsfjkldsfksdjfdsfjfkksfsdkfdsjfjkfdsj
</HeaderTemplate>
..........
=============

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
S

Steven Cheng[MSFT]

Thanks for your reply Rkbnair,

The sorting problem is due to you use <HeaderTemplate>, of course you can
remove it if you want it to be autogenerated(with sorting link). And what
you need to specify width is the <HeaderStyle> element e.g.

=========
<asp:TemplateField HeaderText="used" SortExpression="used">
<HeaderStyle Width="100px" />
.........
==========

Also, for BoundField, you can apply "HeaderStyle" or "ItemStyle" as well:

=====================
<asp:BoundField .................>
<ItemStyle Width="100px" />
</asp:BoundField>
================

In addition, the different between the above setting and the snippet you
provided is that in your code snippet, you specify the witdth for those
controls(Label or TextBox) inside the GridView column. However, that
doesn't influence the width of the container GridView column cell.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: =?Utf-8?B?cmtibmFpcg==?= <[email protected]>
Subject: RE: To set Column Width for Gridview
Date: Fri, 2 Nov 2007 09:33:00 -0700

The problem with this syntax is that I cannot make the column sortable by
clicking on the header.

What is the difference if I specify width in the following way:

<asp:templatefield
HeaderText="Complex ID"
SortExpression="str_complex_id"><itemtemplate><asp:Label
ID="gLabel_str_complex_id" Width="200px" runat="server"
Text='<%# Bind("str_complex_id") %>'> </asp:Label></itemtemplate>
<edititemtemplate><asp:TextBox ID="gTextBox_str_complex_id" Width="200px"
runat="server"
MaxLength="16"
Text='<%# Bind("str_complex_id")%>' OnChange="upperCase(this.id)">
</asp:TextBox>
</edititemtemplate>
<footertemplate><asp:TextBox ID="gfTextBox_str_complex_id" Width="200px"
runat="server"
MaxLength="16"
Text='<%# Bind("str_complex_id")%>' OnChange="upperCase(this.id)">
</asp:TextBox>
</footertemplate>
</asp:templatefield>
[/QUOTE][/QUOTE]
 
G

Guest

It works fine. However, the height cannot be reduced from 25 px. I can
increase the height as much as I want though.


--
test


Steven Cheng said:
Thanks for your reply Rkbnair,

The sorting problem is due to you use <HeaderTemplate>, of course you can
remove it if you want it to be autogenerated(with sorting link). And what
you need to specify width is the <HeaderStyle> element e.g.

=========
<asp:TemplateField HeaderText="used" SortExpression="used">
<HeaderStyle Width="100px" />
.........
==========

Also, for BoundField, you can apply "HeaderStyle" or "ItemStyle" as well:

=====================
<asp:BoundField .................>
<ItemStyle Width="100px" />
</asp:BoundField>
================

In addition, the different between the above setting and the snippet you
provided is that in your code snippet, you specify the witdth for those
controls(Label or TextBox) inside the GridView column. However, that
doesn't influence the width of the container GridView column cell.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
[/QUOTE]
 
S

Steven Cheng[MSFT]

Hi Rkbnair,

Yes, the height will have a low bound due to the contained text's font
size. However, if you find that there is still some extra spaces within
table cell or around it, you can try setting Table(GridView)'s
"CellPadding" and "CellSpacing" to 0, e.g.

===============
<asp:GridView ID="GridView1" runat="server" ...............
CellPadding="0" CellSpacing="0">
====================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
From: =?Utf-8?B?cmtibmFpcg==?= <[email protected]>
Subject: RE: To set Column Width for Gridview
Date: Mon, 5 Nov 2007 07:37:01 -0800
It works fine. However, the height cannot be reduced from 25 px. I can
increase the height as much as I want though.
 

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