Tuesday, 25 October 2011

Set Color of GridLines in Gridview

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.

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


Second, set the color in the OnRowDataBound handler method:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

        foreach (TableCell tc in e.Row.Cells)

            tc.Attributes["style"] = "border-color: #c3cecc";


in VB:
  Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As      System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        For Each tc As TableCell In e.Row.Cells
            tc.BorderColor = Drawing.Color.YellowGreen


    End Sub