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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top