Best control to use to display data as a table

R

Ralph

What would be the best control to use if I wanted to display data in
say a 4x4 cell table.
Or should I consider writing my own control.
I don't think repeater or gridview can quite perform this
functionality,
Although I could nest them.
 
R

Ralph

Does the GridView not do what you want, then...?


Nest them...? Please explain in much more detail what you are trying to
do...

My explanation was bad.
In a grid view you can bind it to a datatable then display each row as
a row in the gridview. Which would put each item in a separate cell
in an html table. What I would like to do is take the entire row from
the datatable I am binding to and display it all in one cell of
table. Then display the next set of data in the adjacent cell. I
would like to have a 4x4 table appear on each page.
For example each cell in the table may contain a picture some display
data and possibly a link.
 
W

William Niver

I would use a custom server control for such a display. I threw this
together to
give you an idea, hope it helps.

William

PS

This code goes in the App_Code folder. I named the file Ralph.vb
You then need to register the namespace in your web.config file

Example:

<controls>
<add tagPrefix="RalphControls" namespace="RalphControls" />
</controls>

The control accepts 2 parameters, the DataSource (as a DataTable) and the
number of columns
you want displayed in a table row.

Again, it's just thrown together and can use improvement, but it may open
you up to new types of solutions.

///////////////////////////////////////////////////////////////

Imports Microsoft.VisualBasic

Namespace RalphControls

Public Class QuadViewDataViewer
Inherits CompositeControl

Protected Overrides Sub CreateChildControls()
MyBase.Controls.Clear()
If DataSource IsNot Nothing AndAlso DataSource.Tables(0) IsNot
Nothing Then
Dim Table_Main As New HtmlGenericControl("table")
With Table_Main
'Add any Table Attributes you require
With .Attributes
.Add("cellspacing", "0px")
End With
'Add any Table Styles you require
With .Style
.Add("border", "1px solid black")
End With
'Loop through each row in your DataSource
Dim RowCounter As Integer = 0
For Each DR As System.Data.DataRow In
DataSource.Tables(0).Rows
'Determine when to start a new row.
If RowCounter = 0 OrElse (RowCounter - 1) Mod
NumberOfColumns = 0 Then
.Controls.Add(New LiteralControl("<TR>"))
End If
Dim Table_Main_TableCell As New
HtmlGenericControl("td")
With Table_Main_TableCell
Dim ColumnOne As String = CStr(DR("Column1"))
Dim ColumnTwo As String = CStr(DR("Column2"))
Dim ColumnThree As String = CStr(DR("Column3"))
Dim ColumnFour As String = CStr(DR("Column4"))
'Create your "Quad View" Table.
Dim Table_QuadView As New
HtmlGenericControl("table")
With Table_QuadView
Dim Table_QuadView_FirstRow As New
HtmlGenericControl("tr")
With Table_QuadView_FirstRow
Dim Table_QuadView_FirstRow_ColumnOne As
New HtmlGenericControl("td")
With Table_QuadView_FirstRow_ColumnOne
'Create your output for the top-left
column.
'In this sample, I am just
outputting the text found in the
'datacolumn, but you can create any
control you desire.
.Controls.Add(New
LiteralControl(ColumnOne))
End With
.Controls.Add(Table_QuadView_FirstRow_ColumnOne)
Dim Table_QuadView_FirstRow_ColumnTwo As
New HtmlGenericControl("td")
With Table_QuadView_FirstRow_ColumnTwo
'Create your output for the
top-right column.
.Controls.Add(New
LiteralControl(ColumnTwo))
End With
.Controls.Add(Table_QuadView_FirstRow_ColumnTwo)
End With
.Controls.Add(Table_QuadView_FirstRow)
Dim Table_QuadView_SecondRow As New
HtmlGenericControl("tr")
With Table_QuadView_SecondRow
Dim Table_QuadView_SecondRow_ColumnOne
As New HtmlGenericControl("td")
With Table_QuadView_SecondRow_ColumnOne
'Create your output for the
bottom-left column.
.Controls.Add(New
LiteralControl(ColumnThree))
End With
.Controls.Add(Table_QuadView_SecondRow_ColumnOne)
Dim Table_QuadView_SecondRow_ColumnTwo
As New HtmlGenericControl("td")
With Table_QuadView_SecondRow_ColumnTwo
'Create your output for the
bottom-right column.
.Controls.Add(New
LiteralControl(ColumnFour))
End With
.Controls.Add(Table_QuadView_SecondRow_ColumnTwo)
End With
.Controls.Add(Table_QuadView_SecondRow)
End With
.Controls.Add(Table_QuadView)
End With
.Controls.Add(Table_Main_TableCell)
'Determine when to end a row.
If RowCounter Mod NumberOfColumns = 0 Then
.Controls.Add(New LiteralControl("</TR>"))
End If
RowCounter += 1
Next DR
End With
'Add teh Table to your control
MyBase.Controls.Add(Table_Main)
End If
End Sub

Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
MyBase.RenderContents(writer)
End Sub

Protected m_DataSource As System.Data.DataSet = Nothing
Public Property DataSource() As System.Data.DataSet
Get
Return m_DataSource
End Get
Set(ByVal value As System.Data.DataSet)
m_DataSource = value
End Set
End Property

Protected m_NumberOfColumns As Integer = 2
Public Property NumberOfColumns() As Integer
Get
Return m_NumberOfColumns
End Get
Set(ByVal value As Integer)
m_NumberOfColumns = value
End Set
End Property

End Class

End Namespace

////////////////////////////////////////////////////////////////
 
R

Ralph

I would mostly recommend the ListView (if you are on 3.5)

Greets,
John.

Thanks to all who posted.
John I believe that is what I will use. I am using net 3.5, although
for some reason I was still thinking of the net 1.1 data list control.
Mark you make a very valid point though I may just go with 1 line of
data as opposed to what I am trying to do.
William when I have time I will try to write my own control thanks for
the startup code though.
This project I am working on is a volunteer project I am working to
make a website to help find homes for stray cats. So I would like to
get something on the web as soon as possible so I can start showing
people the cats that are needing homes.
 
G

Guest

Thanks to all who posted.
John I believe that is what I will use.  I am using net 3.5, although
for some reason I was still thinking of the net 1.1 data list control.
Mark you make a very valid point though I may just go with 1 line of
data as opposed to what I am trying to do.
William when I have time I will try to write my own control thanks for
the startup code though.
This project I am working on is a volunteer project I am working to
make a website to help find homes for stray cats.  So I would like to
get something on the web as soon as possible so I can start showing
people the cats that are needing homes.

I think you can use DataList control. If RepeatDirection is Horizontal
and RepeatColumns is 4, the items are rendered in rows containing 4
columns.

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/datalist.aspx
http://www.eggheadcafe.com/tutorial...b2-a3db1af393f5/aspnet-datalist-and-data.aspx
 
E

Ed Murphy

Ralph said:
My explanation was bad.
In a grid view you can bind it to a datatable then display each row as
a row in the gridview. Which would put each item in a separate cell
in an html table. What I would like to do is take the entire row from
the datatable I am binding to and display it all in one cell of
table. Then display the next set of data in the adjacent cell. I
would like to have a 4x4 table appear on each page.

Do you mean "4x4 table appear in each cell of an outer table"?
 

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,051
Latest member
CarleyMcCr

Latest Threads

Top