Live Chat Icon For mobile
Live Chat Icon

How to create a table control dynamically to populate data

Platform: ASP.NET| Category: Table Control

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

VB.NET


Dim mycn As SqlConnection
Dim myda As SqlDataAdapter
Dim ds As DataSet
mycn = New SqlConnection('server = localhost;uid=sa;password=;database=northwind')
myda = New SqlDataAdapter('Select Employeeid, FirstName , LastName from employees', mycn)
ds = New DataSet
myda.Fill(ds, 'Employees')

Dim dc As DataColumn
Dim dr As DataRow
For Each dr In ds.Tables(0).Rows
	Dim trow As New TableRow
	For Each dc In ds.Tables(0).Columns
		Dim tcell As New TableCell
		tcell.Controls.Add(New LiteralControl(dr(dc.ColumnName).ToString))
		trow.Cells.Add(tcell)
	Next
Table1.Rows.Add(trow)
Next

C#


SqlConnection     mycn ; 
SqlDataAdapter myda ; 
DataSet ds ; 
mycn = new SqlConnection('server = localhost;uid=sa;password=;database=northwind');
myda = new SqlDataAdapter('Select Employeeid, FirstName , LastName from employees', mycn);
ds = new DataSet();
myda.Fill(ds, 'Employees');

TableRow trow ;
TableCell tcell;
foreach  (DataRow dr in ds.Tables[0].Rows)
{
	trow = new TableRow ();  
	foreach( DataColumn dc in ds.Tables[0].Columns)
	{
	tcell= new  TableCell ();
	tcell.Controls.Add(new LiteralControl(dr[dc.ColumnName].ToString()));
	trow.Cells.Add(tcell);
	}
Table1.Rows.Add(trow);
}

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

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

Please submit your question and answer.