Live Chat Icon For mobile
Live Chat Icon

How to use a Table control to display data vertically

Platform: ASP.NET| Category: Table Control

<asp:Table id='Table1' runat='server'></asp:Table>

VB.NET


’Populate the DataSet ds
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  

C#


//Populate the DataSet ds
foreach(DataColumn  dc in ds.Tables[0].Columns )
{
	TableRow trow = new TableRow(); 
	TableCell tcellcolname = new TableCell() ;
	foreach(DataRow 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())) ;
	}
	trow.Cells.Add(tcellcolname) ;
	//To Display the Column Data 
	foreach(DataRow dr in ds.Tables[0].Rows )
	{
		TableCell tcellcoldata =new TableCell() ;
		//Populate the TableCell with the Column Data 
		tcellcoldata.Controls.Add(new LiteralControl(dr[dc.ColumnName].ToString())) ;
		trow.Cells.Add(tcellcoldata);
	}
	Table1.Rows.Add(trow) ;
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.