Confusion: TableRowCollection

R

Rob Meade

Hi all,

I am just making a few methods in my base class, one of which returns a
horizontal line, formatted on the page as *I* want it..its basically a
collection of three table rows.

I discovered the TableRowCollection this evening, so I was hoping that I
could return one of these objects with my three rows inside it, and then
assign the collection to the table...something like...

Dim Table as Table

Table.Rows.Add(CreateHorizontalLine())


Private Function CreateHorizontalLine() As TableRowCollection

Dim TableRowCollection as TableRowCollection

....

Return TableRowCollection

End Function

Unfortunately - this doesn't seem to work - have I misinterpreted the use of
this object or can someone guide me in the right direction please...

I could of course just return a table and add that to a cell in a row in the
table, but this does kinda make unnecessary (maybe) nesting...

Any help appreciated.

Regards

Rob
 
S

S. Justin Gengo

Rob,

Table.Rows.Add is meant to add a single row. You'll have to loop through the
collection and add each row one at a time. Or search out a method that
allows all the rows to be added at once, but I don't think there is one...

As an alternative how about having your method add each row to the table as
passed into your method? If you pass the table to the method ByRef then the
rows could be added to it inside of the method itself.

Public Sub CreateHorizontalLine(ByRef table As Table)
'---Loop to create necessary rows
For
'---Create row
Dim Row As New Row

'---Add row to table
Table.Rows.Add(Row)
Next
End Sub

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
K

Karl Seguin

I would expect it to work. You really didn't say what isn't working?

Can you give a simplified version of your complete code? Your snipet has a
lot of things wrong with it as-is...like you aren't creating a NEW table or
a NEW TableRowCollection, and your table is never added to a control or
anything...

Karl
 
G

Guest

The add method takes only onle object. You may create an array of TableRows
and use the AddRange instead:
table1.Rows.AddRange(CreateHorizontalLine())

Private Function CreateHorizontalLine() As TableRow()
Dim ret(1) As TableRow 'creates an array of 2 elements of type
tablerow
ret(0) = New TableRow
ret(0).Cells.Add(New TableCell)
ret(1) = New TableRow
ret(0).Cells.Add(New TableCell)
Return ret
End Function
 
R

Rob Meade

...
As an alternative how about having your method add each row to the table
as passed into your method? If you pass the table to the method ByRef then
the rows could be added to it inside of the method itself.

Hi there,

Yeah, that had occured to me also, didn't want to tie it down to the table
really (at the time of writing the method) - although, when I think about it
now it would still be dynamic as the table identifier would be passed in,
plus, the rows are only ever going to be added to a table, nothing else, so
it makes good sense.

Many thanks for your reply.

Regards

Rob
 
R

Rob Meade

in ...
I would expect it to work. You really didn't say what isn't working?

Can you give a simplified version of your complete code? Your snipet has
a lot of things wrong with it as-is...like you aren't creating a NEW table
or a NEW TableRowCollection, and your table is never added to a control or
anything...

You dont new it in apparantly, it has no constructor.

Code snippet:

......from my page....

Private Function DisplayFirst(ByVal firstID As Integer) As Boolean

' declare variables
Dim First As First
Dim Row As TableRow
Dim Cell As TableCell

' create new instances of our objects
First = New First(firstID)
Row = New TableRow
Cell = CreateCell(100, True)

' define cell properties
Cell.Text = "My First " & First.Title & " (" & First.DateTime & ")"

' add cell to row
Row.Cells.Add(Cell)

' add row to table
tblFirst.Rows.Add(Row)

' TODO: Add horizontal line here
' This is only an example of what I had tried, I have
currently removed this code in order to make this work!
tblFirst.Rows.Add(CreateHorizontalLine())

' I have also tried - addRange which didn't work either
tblFirst.Rows.AddRange(CreateHorizontalLine())


' create new instances of our objects
Row = New TableRow
Cell = CreateCell(1, True)

' define cell properties
Cell.Text = First.Description

' add cell to row
Row.Cells.Add(Cell)

' add row to table
tblFirst.Rows.Add(Row)

Return True


End Function


......from my base class...

Public Function CreateHorizontalLine(ByVal lineColor As String) As
TableRowCollection

' declare variables
Dim RowCollection As TableRowCollection
Dim Row As TableRow
Dim Cell As TableCell

' set object
RowCollection = Nothing

' create new instances of our objects
Row = New TableRow
Cell = CreateCell(100, True)

' define cell properties
Cell.Controls.Add(CreateShimImage(1, 5))

' add cell to row
Row.Cells.Add(Cell)

' add row to collection
RowCollection.Add(Row)

' create new instances of our objects
Row = New TableRow
Cell = CreateCell(100, True)

' define cell properties
Cell.BackColor = Drawing.Color.FromName(lineColor)
Cell.Controls.Add(CreateShimImage(1, 1))

' add cell to row
Row.Cells.Add(Cell)

' add row to collection
RowCollection.Add(Row)

' create new instances of our objects
Row = New TableRow
Cell = CreateCell(100, True)

' define cell properties
Cell.Controls.Add(CreateShimImage(1, 5))

' add cell to row
Row.Cells.Add(Cell)

' add row to collection
RowCollection.Add(Row)

' return rowcollection
Return RowCollection

End Function
 
R

Rob Meade

...
The add method takes only onle object.

Thats why I was hoping I could push in a "collection" - thus one object
(containing many objects)..
You may create an array of TableRows and use the AddRange instead:

hmm...I saw that, and tried it - and it didn't work?!

I've just posted my example code in a reply to Karl if you wanna see it.
table1.Rows.AddRange(CreateHorizontalLine())

Private Function CreateHorizontalLine() As TableRow()
Dim ret(1) As TableRow 'creates an array of 2 elements of type
tablerow
ret(0) = New TableRow
ret(0).Cells.Add(New TableCell)
ret(1) = New TableRow
ret(0).Cells.Add(New TableCell)
Return ret
End Function

Ahhh...now that's a bit different to what I had - you've got an array...I
didn't try it like that - I used the TableRowCollection object (thought it
was aptly named etc)!..

I'll give you example a go..

Regards

Rob
 
R

Rob Meade

...
The add method takes only onle object. You may create an array of
TableRows
and use the AddRange instead:
table1.Rows.AddRange(CreateHorizontalLine())

Private Function CreateHorizontalLine() As TableRow()
Dim ret(1) As TableRow 'creates an array of 2 elements of type
tablerow
ret(0) = New TableRow
ret(0).Cells.Add(New TableCell)
ret(1) = New TableRow
ret(0).Cells.Add(New TableCell)
Return ret
End Function

Hi Philip,

Tried it = it gives me:

Object reference not set to an instance of an object.

Any thoughts?
 
G

Guest

Hi Rob,

The code segment that I gave to you is dependant on having a <asp:Table
ID="table1" Runat="server"> tag in the markup of page class and a protected
variable in the code behind:
Protected WithEvents table1 As System.Web.UI.WebControls.Table

You should probably replace the variable named table1 with the equivalent in
your code.
 
R

Rob Meade

...
Hi Rob,

The code segment that I gave to you is dependant on having a <asp:Table
ID="table1" Runat="server"> tag in the markup of page class and a
protected
variable in the code behind:
Protected WithEvents table1 As System.Web.UI.WebControls.Table

You should probably replace the variable named table1 with the equivalent
in
your code.

Hi Philip,

Yes, I have that...my table was dragged onto the page and named "tblFirst"..

In my code I have this:

tblFirst.Rows.AddRange(CreateHorizontalLine("#DDDDDD"))

and this is the line that errors, it seems to want me to new in the
CreateHorizontalLine object....

Any thoughts?

Rob
 
G

Guest

Hi Rob,

Make sure the CreateHorizontalLine function returns a new array of
tablerows. I tried this code on my desktop and I certify that it is bug-free
:)

Private Function CreateHorizontalLine() As TableRow()
Dim ret(1) As TableRow 'creates an array of 2 elements of type
tablerow
Dim cell As TableCell
ret(0) = New TableRow
cell = New TableCell
cell.Text = "Line1"
ret(0).Cells.Add(cell)
ret(1) = New TableRow
cell = New TableCell
cell.Text = "Line2"
ret(1).Cells.Add(cell)
Return ret
End Function
 
R

Rob Meade

...
Make sure the CreateHorizontalLine function returns a new array of
tablerows. I tried this code on my desktop and I certify that it is
bug-free
:)

hehe, thanks for the continued support :eek:)

turns out - I'm stoooooooooooooooooopid :eek:)

I had the code you have, albeit slightly changed to meet my needs, however,
I had dimed in the size of the array as 3 - for 3 rows...

I then created 3 rows and added them...

Turns out - I had 4 spaces in my array, and only filled three of
them....damn the zero-based-ness of it all! :eek:D

Thanks for your help - works a treat....I now have a nice grey line under my
"incorrectly" formatted date (see other thread) ;o)


Regards

Rob
 
G

Guest

Hi Rob,

In ASP.NET2.0, you should take advantage of the following page directive
attributes
Culture="auto" UICulture="auto" which allow your browser setting to take
control of the display format.

Just create a simple page as this:
<%@ Page Culture="auto" UICulture="auto" Language="C#"
CompileWith="AutoCulture.aspx.cs" ClassName="AutoCulture_aspx" %>

<html>
<body>
<form runat="server">
<h2><asp:Label ID="Label1" Runat="server" /></h2>
</form>
</body>
</html>

and in the codebehind add

void Page_Load (object sender, EventArgs e)
{
Label1.Text = "Today's date is " +
DateTime.Now.ToShortDateString();
}

Then open the browser and change the language settings (Tools->Internet
Options->languages-->Add English [en-gb] and move it up). Then go to the
page that you just created and watch the date display matching your browser
language setting.
 
R

Rob Meade

...

[snip]

I created the small example you gave me...it produced this...

"Today's date is 11/21/2005"

When I look under Internet Options - Tools - General - Languages - English
is the only item in the list and it already is en-GB...

Any ideas why I am getting the format in US?

Regards

Rob
 
R

Rob Meade

...
Hi Rob,

I put a sample for you at this link:
http://www.webswapp.com/demos/autoculture.aspx

When you enter the page you should see your culture setting being
displayed.
If you change your browser settings the date format and the culture name
will be displayed accordingly.

Hi Phillip,

Thanks for the demo, and it worked perfectly, displayed the date/time
correctly and it changed when I changed my language settings in IE...

So - my browser is obviously ok then! So whats up with my code?! Does this
mean its a problem with my laptop region settings or something? As no matter
what I do the out put keeps coming out the same...

Any further help is appreciated...

Rob
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top