Set DataGrid column width from code-behind

J

JL

Hi all,

I have a DataGrid that is binded from a dataset where the number of columns
is unknown. Therefore I have not set up the columns at design time, but I
would like to control the width of each column.

I have wrote the following code:

Protected WithEvents MyGrid As System.Web.UI.WebControls.DataGrid
Dim GridColumn As System.web.UI.WebControls.TemplateColumn

For Each GridColumn In MyGrid.Columns
GridColumn.HeaderStyle.Width = Unit.Pixel(300)
GridColumn.ItemStyle.Width = Unit.Pixel(300)
Next

....but it does not seem to work. All columns have different widths,
depending on the width of the string inside it.

What is wrong here? Thank you!
 
N

Norman Crandall

You are going to want to set the width in the onItemDataBound Event of the
datagrid.

Norm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top