1

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.

1
  • not sure but may be the type of row should the existing table row. or the row should be three column Commented Jan 5, 2013 at 7:27

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is great brother Shahkalpesh... what an excellent knowlage . One small question, why it will not produce another row if I click the button again?!.. I also would take the advantage to ask you if you know how to merge rows if it's equel to previous row in the same column... you are really amzing guys.
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.