Setting the column width on a GridView

Posted in: ASP.NET
By dePoPo
Sep 23, 2009 - 4:05:35 PM

To set the widht of a column on a GridView in ASP.NET you can hook inot the RowDataBound event.

This code detects the header line (datacontrolrowtype.header) and then hooks in to set the width of the columns. Instead of percentages you can use any Unit.* measure you like (pixels, mm)


    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        If e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(0).Width = Unit.Percentage(10)
            e.Row.Cells(1).Width = Unit.Percentage(20) 
            e.Row.Cells(2).Width = Unit.Percentage(10)
            e.Row.Cells(3).Width = Unit.Percentage(60)
        End If

    End Sub


Visitor Comments