// Set the table's formatting-related properties. table1.Border=1; table1.CellPadding=3; table1.CellSpacing=3; table1.BorderColor="red";
// Start adding content to the table. HtmlTableRow row; HtmlTableCell cell; for(int i=1; i<=5; i++) { // Create a new row and set its background color. row =newHtmlTableRow(); row.BgColor=(i%2==0?"lightyellow":"lightcyan"); for(int j=1; j<=4; j++) { // Create a cell and set its text. cell =newHtmlTableCell(); cell.InnerHtml="Row: "+ i.ToString()+"<br />Cell: "+ j.ToString(); // Add the cell to the current row. row.Cells.Add(cell); }
// Add the row to the table. table1.Rows.Add(row); }
// Add the table to the page. this.Controls.Add(table1);
sampath1750
Member
5 Points
55 Posts
How to create table dynamically in asp.net
Apr 26, 2012 08:16 AM|LINK
How to create table dynamically in codebehind and table(for loop or anything) data come from database. Thanks, .
C
gopakumar.r
Participant
959 Points
193 Posts
Re: How to create table dynamically in asp.net
Apr 26, 2012 08:34 AM|LINK
First put a asp:Literal control in you page where you want to show your table, as below
Now in your the event where data is fetched, write the below code to loop though data source and build a table
StringBuilder htmlTable = new StringBuilder(); htmlTable .AppendLine("<table">"); htmlTableString.AppendLine("<tr>"); htmlTableString.AppendLine("<th>colum 1</th>"); htmlTableString.AppendLine("<th>colum 2</th>"); htmlTableString.AppendLine("<th>colum 3</th>"); htmlTableString.AppendLine("</tr>"); /*Put a for loop here and repeat the below code*/ htmlTableString.AppendLine("<tr>"); htmlTableString.AppendLine("<td>colum 1 data</td>"); htmlTableString.AppendLine("<td>colum 2 data</td>"); htmlTableString.AppendLine("<td>colum 3 data</td>"); htmlTableString.AppendLine("</tr>"); /*End For loop*/ htmlTableString.AppendLine("</table>");This will create an HTML string and assign to you literal, thats all!
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
ramiramilu
All-Star
95503 Points
14106 Posts
Re: How to create table dynamically in asp.net
Apr 26, 2012 08:37 AM|LINK
use Gridview...
Thanks,
JumpStart
sampath1750
Member
5 Points
55 Posts
Re: How to create table dynamically in asp.net
Apr 26, 2012 11:18 AM|LINK
How to give colspan and rowspan for that table(if one column data is same in 2 rows then i want merge)
sriramabi
Contributor
4351 Points
1277 Posts
Re: How to create table dynamically in asp.net
Apr 26, 2012 11:25 AM|LINK
try this:
C
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: How to create table dynamically in asp.net
May 02, 2012 10:31 AM|LINK
Displaying a DB table information dynamically ,without using any controls
http://www.dotnetfunda.com/Blogs/Venkyshwe8%40gmail.com/1264/displaying-a-table-information-dynamically-without-using-any-controls
Create a Table Dynamically in ASP.NET
http://www.dotnetcurry.com/ShowArticle.aspx?ID=135&AspxAutoDetectCookieSupport=1
Feedback to us
Develop and promote your apps in Windows Store