Adnan Al-Husain
Adnan Al-Husain

Reputation: 110

Adding row to an existing html table from code behind

more of newbies :)

I've created HTML table using below html code

 <table runat="server" id="Table1" border="1" class="style1">
    <tr>
        <td class="style2" colspan="3">
            Info.</td>
        <td class="style2" colspan="2">
            TypeA</td>
        <td class="style2">
            TypeB</td>
        <td class="style2" rowspan="2">
            TypeC</td>
    </tr>
    <tr>
        <td class="style3">
            Dept.</td>
        <td class="style3">
            Div.</td>
        <td class="style3">
            Unit</td>
        <td class="style3">
            TypeA.1</td>
        <td class="style3">
            TypeA.1</td>
        <td style="text-align: center">
            TypeB.1</td>
    </tr>
</table>

I tried to add row for it using

protected void Button1_Click(object sender, EventArgs e)
{
    TableRow tRow = new TableRow();
    for (int i = 1; i < 8; i++)
    {
        TableCell tb = new TableCell();
        tb.Text = "text";
        tRow.Controls.Add(tb);
    }
    Table1.Rows.Add(tRow);
}

but I got :

The best overloaded method match for 'System.Web.UI.HtmlControls.HtmlTableRowCollection.Add(System.Web.UI.HtmlControls.HtmlTableRow)' has some invalid arguments

I googled around but with no luck.

I'm doing this to avoid Columns Heading merging action in code behind while I still have to look for how to merge rows heading. This is the last step for completing https://stackoverflow.com/questions/13670384/asp-net-dynamic-input-display-table-by-section-with-multi-columns-rows-headers

,.. closer heading view is:

enter image description here

Rows created will have a forwarding heading cell that should be merged if it's the same with row above. Appreaciate the great assistant from StackOverflow members.

Upvotes: 1

Views: 26873

Answers (2)

hojjat.mi
hojjat.mi

Reputation: 1534

You just need more attention. You can try this:

Step 1:

<table runat="server" id="Table1" border="1" class="style1">
    <tr>
        <td class="style2" colspan="3">
            Info.</td>
        <td class="style2" colspan="2">
            TypeA</td>
        <td class="style2">
            TypeB</td>
        <td class="style2" rowspan="2">
            TypeC</td>
    </tr>
    <tr>
        <td class="style3">
            Dept.</td>
        <td class="style3">
            Div.</td>
        <td class="style3">
            Unit</td>
        <td class="style3">
            TypeA.1</td>
        <td class="style3">
            TypeA.1</td>
        <td style="text-align: center">
            TypeB.1</td>
    </tr>
</table>

Step 2 : in code behind use this code:

 HtmlTableRow tRow = new HtmlTableRow();
 for (int i = 1; i < 3; i++)
 {
    HtmlTableCell tb = new HtmlTableCell();               
    tb.InnerText = "text";
    tRow.Controls.Add(tb);
 }
 Table1.Rows.Add(tRow);

Notice: Use this using in code behind:

using System.Web.UI.HtmlControls;

Upvotes: 1

shahkalpesh
shahkalpesh

Reputation: 33476

The error is apparent in the exception.

You are using TableRow instead of HtmlTableRow.
Hover over TableRow tRow = new TableRow(); to see which namespace is the TableRow class from.

A similar change for cell would be required. i.e. use HtmlTableCell instead of TableCell.

EDIT: Table, TableRow are classes from System.Web.UI.WebControls namespace.
Whereas, HtmlTable, HtmlTableRow are classes from System.Web.UI.HtmlControls namespace.

Upvotes: 5

Related Questions