Controlling column width in a GridView

Security Briefs

Syndication

I've been building some internal pages for our sales team here at Pluralsight, and many of those pages make use of the ASP.NET GridView control to display rectangular data. It's generally a really easy to use control, but I've always struggled with getting column widths to look right.

My goal is to fix the width of each column at design time, and any field that contains text that may be longer than my fixed width should wrap around, taking up more vertical space in the table. If you are trying to accomplish this goal, you might find these tips helpful.

1) Set up a CssClass for the GridView itself and include the table-layout:fixed style. This tells the browser that you're going to specify the width of each cell. You may also want to include the overall width of the grid here as I mention in (3).

2) The first row of the table sets the width for each cell, and that's usually the HEADER row, not the item row, so use either HeaderStyle-CssClass or HeaderStyle-Width to set the width of the cell. I wasted a lot of time trying to set the width using the ItemStyle.

3) Make certain the table itself is wide enough to hold all of the cells. I added up all of my cell widths and used that to set the width via the CssClass attribute on the GridView.

Using these guidelines, I'm having much better luck controlling the layout of my GridView controls. I hope this simple advise helps someone else!


Posted Jul 30 2008, 10:23 AM by keith-brown
Filed under: ,

Comments

sun wrote re: Controlling column width in a GridView
on 07-31-2008 6:31 AM

thanks i was looking for this...

Prajith wrote re: Controlling column width in a GridView
on 08-13-2008 6:32 AM

Thanks a lot Keith

I ahve browsed abround 100 sites and all are giving different options and none of them worked for me. And finally your article helped me to sort out the issue. I really appreciate your work

naxtell wrote re: Controlling column width in a GridView
on 08-22-2008 1:41 PM

I did exactly what you explained a while back.  Then I implemented paging and the fixed column widths no longer work right because now the first row contains a TD with a colspan of the number of columns.  All of the columns take on an equal width.  Do you know of a way to do as you describe, but when Paging is implemented?

Thanks.

subhash wrote re: Controlling column width in a GridView
on 09-04-2008 1:33 AM

Can u provide  CssClass for the GridView

keith-brown wrote re: Controlling column width in a GridView
on 09-04-2008 7:13 AM

naxtell,

I've not yet tried to solve that problem - let us know if you find a solution!

subhash,

Here's what my .Grid CSS class looks like:

.Grid
{
  table-layout: fixed;
  width: 70em;
}
Olin wrote re: Controlling column width in a GridView
on 09-25-2008 8:05 AM

Good, I was looking for your solution for 3 days... Thanks a lot

svk wrote re: Controlling column width in a GridView
on 01-13-2009 5:22 AM

i am trying to set the gridview width at design time to fit the page

i have set the headerstyle width in 'px

but the width gets fitted only in 75% of the page width

if i remve the headerstyle with it gets fitted poroperly

Eswar wrote re: Controlling column width in a GridView
on 01-13-2009 12:49 PM

Thanks. A good article.

ujjwal wrote re: Controlling column width in a GridView
on 01-15-2009 4:48 AM

good job kaith. That Css worked for me

SVK wrote re: Controlling column width in a GridView
on 01-16-2009 1:18 AM

is it  possible to set the column width without table-layout: fixed;

if i want to do it how wuld i

Manish wrote re: Controlling column width in a GridView
on 01-16-2009 2:36 AM

Thanks man...table-layout:fixed saved me!! :)

Vaishali wrote re: Controlling column width in a GridView
on 01-30-2009 12:14 AM

Hi ... thanks i have been struggling for this ... however ..the cell text does not wrap around !!! ... please help

Moses M wrote re: Controlling column width in a GridView
on 01-30-2009 3:08 AM

Thanks Dude...It is only solution to set the width of the Gridview fixed.

Bhavpreet Singh Narang wrote re: Controlling column width in a GridView
on 02-25-2009 10:21 PM

Thanks a million dude..