471,337 Members | 1,161 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

calculating the height of a dynamically generated table

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.
Jul 21 '05 #1
3 2192
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.
"rufus" <ru***@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
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.

Jul 21 '05 #2
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

"Elliot Rodriguez" <el**************@hotmail.com> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
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.
"rufus" <ru***@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
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.


Jul 21 '05 #3
Thanks Elliot,

Your code really helped. Exactly what I was looking for.

"Elliot Rodriguez" wrote:
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

"Elliot Rodriguez" <el**************@hotmail.com> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
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.
"rufus" <ru***@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
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.



Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Just Dummy | last post: by
2 posts views Thread by Robson Carvalho Machado | last post: by
3 posts views Thread by RJN | last post: by
2 posts views Thread by Steve Bottoms | last post: by
3 posts views Thread by Peter | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.