ASP.NET Remove GridView Border From EmptyDataTemplate

by Bill Beckelman 8. February 2008 18:34

I like to put a border around my gridviews. However, when they are empty, the border doesn't look good. It took some searching to find a good way to not show the border. Its been a while, so I am not sure which forum I found the answer in. I improved upon the answer though by adding the code to a class, so I thought I would share it.

image

image

I added the code below to a class that contains some other gridview stuff:

    Public Shared Sub RemoveBorders(ByVal gridView As GridView)
        If gridView.Rows.Count = 0 Then
            gridView.BorderStyle = BorderStyle.None
        Else
            gridView.BorderStyle = BorderStyle.Solid
        End If
    End Sub

As pointed out by Tilo in the comments I changed from the OnRowDataBound event to the Databound Event of the Gridview.

Then in the DataBound Event of all of my gridviews that I want to remove the border from, I add the following code (SortGridView is the name of my class):

    Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        SortGridView.RemoveBorders(sender)
    End Sub

Hopefully, I can save somebody some time looking through the forums.

Tags:

ASP.NET

Comments


About Me

I live and work in Salt Lake City, Utah. My background is in aviation. I have a degree in Aeronautical Science from Embry-Riddle Aeronautical University in Prescott, AZ. I have worked as a commercial airline pilot and most recently as a technical advisor for a charter airline.