ASP.NET ships with 45 built in Server Controls. These controls are capable of exposing an object model containing properties, methods, and events. ASP.NET developers can use this object model to cleanly modify and interact with the page.
This code with respect to one of the queries in the newsgroup. Normally we get the data displayed in the following manner :
Header1 |
Header2 |
Header3 |
Data |
Data |
Data |
Data |
Data |
Data |
But what if the requirement is to display it as below:
Header1 |
Data |
Data |
Header2 |
Data |
Data |
Header3 |
Data |
Data |
Let's try to achieve this. The control that would be appropriate to display such a kind of page would be Table Control.
The following code is to access the data:
Dim
mycn As SqlConnection
Dim myda As SqlDataAdapter
Dim ds As DataSet
mycn = NewSqlConnection
"server=localhost;uid=sa;password=;database=northwind")
myda = New SqlDataAdapter("Select Employeeid, FirstName , LastName from employees", mycn)
ds = New DataSet()
myda.Fill(ds, "Employees")
To display the data in the desired manner the code is as follows.
Dim
dc As DataColumn
Dim dr As DataRow
For Each dc In ds.Tables(0).Columns
Dim trow As New TableRow
Dim tcellcolname As New TableCell
'To Display the Column Names
For Each dr In ds.Tables(0).Rows
tcellcolname.Text = dc.ColumnName
trow.BackColor = System.Drawing.Color.Beige
tcellcolname.BackColor = System.Drawing.Color.AliceBlue
'Populate the TableCell with the Column Name
tcellcolname.Controls.Add(New LiteralControl(dc.ColumnName.ToString))
Next
trow.Cells.Add(tcellcolname)
'To Display the Column Data
For Each dr In ds.Tables(0).Rows
Dim tcellcoldata As New TableCell
'Populate the TableCell with the Column Data
tcellcoldata.Controls.Add(New LiteralControl(dr(dc.ColumnName).ToString)) trow.Cells.Add(tcellcoldata)
Next
Table1.Rows.Add(trow)
Next
So the output that we get is as follows:
Employeeid |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
FirstName |
Nancy |
Andraw |
Janet |
Margreat |
stevan |
Micheal |
Robart |
Laura |
Anne |
LastName |
Davlivo |
Fuller |
Leavring |
Peacock |
Bucchan |
Suyama |
King |
Callahan |
Dodsworth |