Wednesday, March 18, 2009

The Case of The Polluting GridView

When I use a GridView control on my ASP.NET page, the TABLE tag generated automatically gets the attributes:
cellspacing="0" rules="all" border="0" style="border-collapse:collapse;"

I've tried using:
gdPostedJobs.Attributes.CssStyle.Remove(HtmlTextWriterStyle.BorderCollapse);
gdPostedJobs.Attributes.Remove("rules");

...but they didn't seem to work so here's what I ended up doing.

Getting Rid of the Rules="all" Attribute

Simply set the GridLines property to None in the GridView tag. Example:
<asp:gridview id="gridWExp" runat="server" gridlines="None">

Getting Rid of the CellSpacing="0" and style="border-collapse:collapse;" Attributes

Set the CellSpacing attribute to -1 in the GridView tag. Example:
<asp:gridview id="gridWExp" runat="server" gridlines="None" cellspacing="-1">

The border="0" attribute remained in the generated HTML and I guess the only way to remove it would be to inherit the GridView class and create my own control.


See the rant at VelocityReviews about the GridView

PS: This is my 400th post!

1 comment:

Tejal said...

try to use style with in the gridview control.

or you can do

apply the cellspacing="-1" in the grid view and at serverside make the cellspacing="0"

i applied with the 1st option and its working