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:



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");

Comments (7) -

Lee Dumond
Lee Dumond United States
6/4/2009 6:55:50 PM #

Why not this?

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

agrace United States
6/5/2009 1:36:52 AM #

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

agrace United States
6/5/2009 1:51:33 AM #


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...

Lee Dumond
Lee Dumond United States
6/5/2009 1:00:07 PM #


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.

agrace United States
6/5/2009 1:40:18 PM #

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

sr India
12/21/2011 11:11:37 PM #

Thanks alot

chandu kumar
chandu kumar India
12/19/2012 5:23:31 PM #

both works

Pingbacks and trackbacks (3)+

Add comment

  Country flag
  • Comment
  • Preview