Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add rows to a table control? [Beginner]

Status
Not open for further replies.

DSect

Programmer
Joined
Sep 3, 2001
Messages
191
Location
US
Here's a basic question:

I'm trying to add rows to a table dynamically.

My current code only creates 1 row w/ 2 cells in it:

Code:
        Dim r As New TableRow()
        Dim c As New TableCell()

        c.Controls.Add(New LiteralControl("ROW 1"))
        r.Cells.Add(c)
        tbl.Rows.Add(r)
        c.Controls.Add(New LiteralControl("ROW 2"))
        r.Cells.Add(c)
        tbl.Rows.Add(r)

This displays "ROW 1ROW2" when I'm looking for:
ROW 1
ROW 2

What am I missing here that's not letting it create 2 rows in output?

Thanks!
 
Try the code below. By using a loop the TableRow object is recreated everytime so a new row is added each time round the loop
Code:
Dim r As New TableRow()
Dim c As New TableCell()
Dim i As Int
For i = 1 to 10
  c.Controls.Add(New LiteralControl("ROW " & i.ToString()))
  r.Cells.Add(c)
  tbl.Rows.Add(r)
Next

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Thanks crazyboybert,

I've seen that example used in most places, but I'm unable to figure out why mine doesn't work.

I thought I could Dim a row and cell, add cell(s) to a row, add row to table and repeat.

Here's essentially what yours does when not in a loop:
c.Controls.Add(New LiteralControl("ROW 1"))
r.Cells.Add(c)
tbl.Rows.Add(r)
c.Controls.Add(New LiteralControl("ROW 2"))
r.Cells.Add(c)
tbl.Rows.Add(r)

But if I do that - It doesn't work.. Same as my example above..

I do not want to loop through anything to build the table as each table row will have a line of text that differs for each row. This is more to understand the manual creation as I understand the LOOPing creation.

Here's what I'm trying to do:

Trying to make a table that displays data and does not draw cells for fields when there is no data (that's why I'm using the server control and not HTML tables). The table would look like this:

Column 1 | Column 2
First Name: | [label showing data from DB] (this is a row)
Last Name: | [lable showing data from DB] (this is a row)

It's quite simple, but I don't think I can use the DataList or Datagrid controls because they list the column names across the page, rather than down it (to the best of my knowledge).

If I can figure out why my basic, manually-built doesn't work, I will understand much more about how they work.

As it is now, I know how to dynamically create the table by pulling the text labels from the DB doing a 'SELECT xx AS 'First Name' to create the labels in Column 1..

I would LOVE to know why my manual one doesn't work, even though I can do it programtically.. That way I truely understand what's going on.


 
PS - I got it..
Code:
Dim r As TableRow
Dim c As TableCell
r = New TableRow()
c = New TableCell()
c.Controls.Add(New LiteralControl("ROW 1"))
r.Cells.Add(c)
tblDeptInfo.Rows.Add(r)

r = New TableRow()
c = New TableCell()
c.Controls.Add(New LiteralControl("ROW 2"))
r.Cells.Add(c)
tblDeptInfo.Rows.Add(r)


Hmm.. I need to learn when to Dim something as NEW or when to not use NEW and set it w/ = after dimming.. Object Reference is what that is, correct??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top