views:

763

answers:

2

I have a gridview that I need to get the width of columns for after the gridview is databound. I have to have AutoGenerateColumns=true because the columns for the datatable that the gridview is bound to is not going to be fixed. The problem I'm having is the gridview columns collection is not populated when AutoGenerateColumns is true. Is there anyway I can get the width of each column after the gridview is databound? I've tried using RowDataBound and going through the tablecells but the width is 0 for every column.

A: 

Perhaps this is too early in the control's life cycle to access the GridView's properties. Have you tried accessing the GridView's column width in the DataBound Event? See the ASP.NET Page Life Cycle Overview for more information.

DataBound Event:

This event marks the end of data-binding operations in a data-bound control. In a GridView control, data binding is complete for all rows and any child controls. Use this event to format data bound content or to initiate data binding in other controls that depend on values from the current control's content.

David Glass
Yes I've tried DataBound. It's still 0 in that event.
John
+1  A: 

Unless you are setting the width of the column to a specific size (you aren't) you can't possibly know the width of the generated column until at least the render Page_Render event. The control's html does not exist until then and its width can't be known until the page is rendered into html and the css attached processed.

Joel Etherton