How create dynamic html table from the datatable with rowspan and colspan in ASP.Net

Last Reply on Jul 12, 2012 11:40 PM By Mudassar

Posted on Jul 12, 2012 09:06 AM

HI friends,

How to create dynamic html table from datatable with rowsoan and colspan in asp.net ??

everything in dynamic only even column names also..

thanks in advanced.

Posted on Jul 12, 2012 11:40 PM

Add a Panel on the page

    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>

and then create and add the table to panel

Table table = new Table();
table.BorderStyle = BorderStyle.Solid;

//Add Header Row
TableRow row = new TableRow();
TableHeaderCell headerCell = new TableHeaderCell();
headerCell.Text = "Item";
row.BackColor = Color.Green;

row.Controls.Add(headerCell);
headerCell = new TableHeaderCell();
headerCell.Text = "Price";
row.Controls.Add(headerCell);
table.Controls.Add(row);

//Add DataRow
row = new TableRow();
TableCell cell = new TableCell();
cell.Text = "Shirt";
row.Controls.Add(cell);
cell = new TableCell();
cell.Text = "200";
row.Controls.Add(cell);
row.BackColor = Color.Yellow;
table.Controls.Add(row);

//Add Colspan Row
row = new TableRow();
row.BorderStyle = BorderStyle.Solid;
cell = new TableCell();
cell.Text = "Colspan Row";
cell.ColumnSpan = 2;
row.Controls.Add(cell);
row.BackColor = Color.Brown;
table.Controls.Add(row);

//Add DataRow
row = new TableRow();
cell = new TableCell();
cell.Text = "Tie";
row.Controls.Add(cell);
cell = new TableCell();
cell.Text = "30";
row.Controls.Add(cell);
table.Controls.Add(row);
row.BackColor = Color.Beige;
Panel1.Controls.Add(table);

 

ItemPrice
Shirt 200
Colspan Row
Tie 30