CodersBarn.com
The ASP.NET Community Blog

Set Color of GridLines in Gridview

May 31, 2009 06:04 by agrace

There are times when you will want to set the color of the grid lines in your GridView - however there is not to my knowledge a way of doing this declaratively. A workaround is to do this by tapping into the GridView's RowDataBound event.

GridView Gridlines

 

First, set the OnRowDataBound property in the markup of the GridView:

OnRowDataBound="MyGrid_RowDataBound"

 

Second, set the color in the OnRowDataBound handler method:

protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
     foreach (TableCell tc in e.Row.Cells)
     {
         tc.Attributes["style"] = "border-color: #c3cecc";
     }
}

 

Happy coding :-)

Update 06-01-2009:
See Lee's suggestion in the comments on a quicker way to do this:
Add this to the page Load method: this.GridView1.Attributes.Add("bordercolor", "c3cecc");

kick it on DotNetKicks.com  411ASP.NET


Comments

June 4. 2009 01:28

trackback

Trackback from Web Development Community

Set Color of GridLines in Gridview

Web Development Community

June 4. 2009 01:33

trackback

Trackback from DotNetKicks.com

Set Color of GridLines in Gridview

DotNetKicks.com

June 4. 2009 10:09

trackback

Trackback from DotNetBurner - ASP.net

Set Color of GridLines in Gridview

DotNetBurner - ASP.net

June 4. 2009 18:55

Lee Dumond

Why not this?

    protected void Page_Load(object sender, EventArgs e)
    {
       this.GridView1.Attributes.Add("bordercolor", "c3cecc");
    }

Lee Dumond

June 5. 2009 01:36

agrace

Nice one Lee, I tried it and it works! Smile

agrace

June 5. 2009 01:51

agrace

@Lee,

The weird thing is that there is a bordercolor property available declaratively, except that it only changes the outer border - but the same property, when declared programmatically, changes the internal border color? Odd...

agrace

June 5. 2009 13:00

Lee Dumond

Anthony,

With the GridView, the declarative bordercolor attribute adds an inline style declaration which only applies to the table itself, not individual cells.

Adding the bordercolor attribute programmatically does not use an inline style, but uses the HTML bordercolor property, which browsers apply to ALL borders inside the table.

Lee Dumond

June 5. 2009 13:40

agrace

Thanks for the explanation... that has always bugged me! Smile

agrace

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading