calculating the height of a dynamically generated table

G

Guest

I am created a table dynamically and I have wrapped it in a div tag so that
the user can scroll the table horizontally ie I have added an overflow
property to the div. I dont want the user to be able to scroll vertically so
I need to set the height of the div at runtime based on the number of table
rows generated. Ideally I want the div scrollbars to sit just underneath the
bottom of the table.

I have looked around and haven't found anything to enable me to do this.
Has anybody got any ideas?

Thanks in advance.
 
E

Elliot Rodriguez

If you create the table dynamically, you can set the row's height when you
generate them (use the Height property of the TableRow class). Once you do
that, when youre done adding rows, multiply the number of rows times the
height. That gives you a good estimate on what the total table height is
(not counting Cellpadding or Spacing).

if you created the DIV dynamically (or specify runat server), you can set
the DIV height to the total table height (plus a little more).

I'm going ot see if I can make this work.
 
E

Elliot Rodriguez

See if this helps. Change the array size and uncomment the other elements to
see the changes in height. The ExtraPadding variable should accomodate for
padding, but you will probably want to mess with it yourself.

Private RowHeight As Integer = 20

Private ExtraPadding As Integer = 20

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender

Dim testArray(7) As String

testArray(0) = "item 1"

testArray(1) = "item 2"

testArray(2) = "item 3"

testArray(3) = "item 4"

testArray(4) = "item 5"

testArray(5) = "item 6"

testArray(6) = "item 7"

'testArray(7) = "item 8"

'testArray(8) = "item 9"

'testArray(9) = "item 10"

For Each strItem As String In testArray

Dim Row As New TableRow

Row.Height = Unit.Pixel(RowHeight)

Dim Cell As New TableCell

Cell.Text = strItem

Row.Cells.Add(Cell)

Me.DynamicTable.Rows.Add(Row)

Next

DynamicTable.Width = Unit.Pixel(350)

Dim DivHeight As Integer = (RowHeight * Me.DynamicTable.Rows.Count) +
ExtraPadding

Dim DivTag As New HtmlGenericControl("DIV")

DivTag.Attributes.Add("STYLE", "Height:" & Unit.Pixel(DivHeight).ToString &
"; BACKGROUND-COLOR:gainsboro; WIDTH:200px;OVERFLOW:auto")

DivTag.Controls.Add(DynamicTable)

Page.FindControl("Form1").Controls.Add(DivTag)



End Sub
 

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

Top