DataGrid Column Width is broken - is this a bug?

J

Jeremy

I have googled for this issue, and many other people have the same problem
with no resolution. The data grid column sizes automatically resize based
on their contents no matter what the Style.Width is set to. I was looking
at the HTML output, and the values I entered for Width does not even get
generated.

And to make things worse, even though I set the width of the Grid to 100%,
it actually expands beyond the edge of the screen forcing the user to scroll
horizontally. Here is my code:

Private Sub AddTemplateColumn( _
ByVal InsertAt As Integer, _
ByVal itemTemplate As ITemplate, _
ByVal headerText As String, _
ByVal pixelWidth As Integer)

Dim tempColumn As New TemplateColumn

dgOptions.Columns.AddAt(InsertAt, tempColumn)

With tempColumn
.ItemTemplate = itemTemplate
.HeaderText = headerText
.ItemStyle.Width = _
New Web.UI.WebControls.Unit( _
pixelWidth, _
UnitType.Pixel)
End With
End Sub

This is driving me insane.

Thanks,
Jeremy
 
A

Alvin Bruney [MVP]

you need to remove the width setting on the datagrid control first. this
setting overrides internal settings because cells must be drawn within the
limits imposed by the datagrid control
 
B

bruce barker

it not a bug.

grids build html tables. in html css if you set with of a table cell
narrower than the content with no overflow handling specified, then the
width of the cell is expanded (check the w3c standards). for tables, all
widths are really rending hints. you can set a overflow style on the
contents of the cell and the cell width will be honored.

example:

<table border=1>
<tr>
<td style="width:80pt;">will fit</td>
<td style="width:80pt;">this_cell_is_too_width_and_will_blow_width</td>
</tr>
</table>

<table border=1>
<tr>
<td style="width:80pt">will fit</td>
<td style="width:80pt"><span
style="width:80pt;overflow:hidden">this_cell_is_too_width_and_will_be_clippe
d</span></td>
</tr>
</table>

note: <td>'s do not support the overflow style;


-- bruce (sqlwork.com)
 
J

Jeremy

you need to remove the width setting on the datagrid control first. this
setting overrides internal settings because cells must be drawn within the
limits imposed by the datagrid control

That doesnt make any sense, I WANT the grid to be 100% of the page, but it
is going beyond that.
 
J

Jeremy

grids build html tables. in html css if you set with of a table cell
narrower than the content with no overflow handling specified, then the
width of the cell is expanded (check the w3c standards). for tables, all
widths are really rending hints. you can set a overflow style on the

I thought the same thing, so I put all the text in a 5 char wide text box,
and it still happens, it's just a huge empty cell with a tiny text box with
all the text.

As I said in my original post, the width is not being written to the table,
look at the HTML output.
 
M

Matt Berther

Hello Jeremy,
Private Sub AddTemplateColumn( _
ByVal InsertAt As Integer, _
ByVal itemTemplate As ITemplate, _
ByVal headerText As String, _
ByVal pixelWidth As Integer)
Dim tempColumn As New TemplateColumn

dgOptions.Columns.AddAt(InsertAt, tempColumn)

With tempColumn
.ItemTemplate = itemTemplate
.HeaderText = headerText
.ItemStyle.Width = _
New Web.UI.WebControls.Unit( _
pixelWidth, _
UnitType.Pixel)
End With
End Sub

I think you will need to set the width of the column on the datagrid itself:

ie: dgOptions.Columns.Item(InsertAt).Width = new Web.UI.WebControls.unit(pixelWidth, UnitType.Pixel)
 

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,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top