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 (14) -

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
agrace United States
6/5/2009 1:36:52 AM #

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

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

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

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

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.

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

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

Terry
Terry United States
9/2/2011 5:03:56 AM #

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

Won't work ... not sure why.  The only thing that has been successful was the original post here:

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

Thanks!

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

Thanks alot

galerie d'art
galerie d'art France
1/25/2012 12:38:14 AM #

Thank you for the little trick about GridLines Set Color of Gridview in. Previously, I used the php, but since the asp is faster in terms of production, I am obliged to become familiar with, so I often come on this site to find tips and tricks like this. Thank you again and see you soon!

anum
anum Afghanistan
3/6/2012 11:01:57 PM #

Hi, Nice to see a pretty professionally done blog on the this rich subject.

sohbet
sohbet United States
4/7/2012 6:11:49 PM #

thanks you

Parthiban
Parthiban India
9/26/2012 1:16:23 AM #

Hi , myself parthiban balraj . i need code for applying style css class to dynamically genereted gridview.

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

both works

Prantick Ghosh
Prantick Ghosh India
1/31/2013 2:22:43 AM #


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

It is working.

Also add
MyGrid.BorderColor=Color.FromName("#c3cecc");

Web Developer
Web Developer United States
4/20/2013 1:20:24 AM #

GridView BorderColor add declarative attributes built a style statement, which only applies to the table itself, not the individual cells.

Adding a BorderColor attribute deliberately do not use the inline style, but uses HTML BorderColor property of the browsers used for all bounds of the array.

http://hitechito.com

Pingbacks and trackbacks (3)+

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading