Setting the column width on a gridview column from code

To set the widht of a column on a GridView in ASP.NET you can hook into 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)

VB.NET Example

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

C# Example

protected void GridViewUpdates_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.Header)
        e.Row.Cells[0].Width = Unit.Pixel(110);