5
<table id="tableContent" border="1" runat="server">
    <tr>
        <td colspan="3">
        Record 1
        </td>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>a</td>
        <td>b</td>
        <td>c</td>
    </tr>
    <tr>
        <td>m</td>
        <td>n</td>
        <td>o</td>
    </tr>
    <tr>
        <td colspan="3">
            <input id="Button1" type="button" value="button" />
        </td>
    </tr>
</table>

I have to create above table dynamically in c# I am trying but didnt get

protected void Page_Load(object sender, EventArgs e)
{

    HtmlTableRow row = null;
    HtmlTableCell cell = null;

    for(int i = 0; i < 5; i++)
    {
        row = new HtmlTableRow();
        cell = new HtmlTableCell();
        tableContent.Controls.AddAt(i, row);
        row.Controls.AddAt(i, cell);
        cell.InnerText="1";
    }
}
5
  • Which part do you have problem with? Commented Oct 28, 2012 at 6:57
  • have you tried to take a div and then add the corresponding td tr as string to its innertext Commented Oct 28, 2012 at 6:58
  • dude, you have to tell the requirement or the expected output. there is no purpose listed here. for example, i could tell you just do Response.Write(yourtablestr); and it would technically be dynamic. Commented Oct 28, 2012 at 7:18
  • Sorry, I do not understand your question. May you please describe what you are exactly trying to do? :) Commented Oct 28, 2012 at 7:19
  • I have written HTml table in quetion thats i have to create dynamically in c# Commented Oct 28, 2012 at 7:22

3 Answers 3

9

You can try this code to create table.

First place this markup in your aspx page like

<table id="tableContent" border="1" runat="server"></table>

Then Try This code in Page_Load like

protected void Page_Load(object sender, EventArgs e)
{
    HtmlTableRow row = new HtmlTableRow();
    HtmlTableCell cell = new HtmlTableCell();

    cell.ColSpan =3;
    cell.InnerText = "Record 1";
    row.Cells.Add(cell);
    tableContent.Rows.Add(row);

    row = new HtmlTableRow();
    cell = new HtmlTableCell();

    cell.InnerText = "1";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "2";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "3";
    row.Cells.Add(cell);

    tableContent.Rows.Add(row);

    row = new HtmlTableRow();
    cell = new HtmlTableCell();

    cell.InnerText = "a";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "b";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "c";
    row.Cells.Add(cell);

    tableContent.Rows.Add(row);


    row = new HtmlTableRow();
    cell = new HtmlTableCell();
    cell.InnerText = "m";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "n";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "o";
    row.Cells.Add(cell);

    tableContent.Rows.Add(row);

    row = new HtmlTableRow();
    cell = new HtmlTableCell();

    HtmlInputButton input = new HtmlInputButton();
    input.ID = "Button1";
    input.Value = "button";

    cell.ColSpan = 3;
    cell.Controls.Add(input);
    row.Cells.Add(cell);
    tableContent.Rows.Add(row);
}

Or You can try this, by storing cell values in 2D Array like

protected void Page_Load(object sender, EventArgs e)
{
    String[,] cellValues = { { "1", "2", "3" }, { "a", "b", "c" }, { "m", "n", "o" } };

    HtmlTableRow row = new HtmlTableRow();
    HtmlTableCell cell = new HtmlTableCell();

    cell.ColSpan = 3;
    cell.InnerText = "Record 1";
    row.Cells.Add(cell);
    tableContent.Rows.Add(row);

    for (int i = 0; i < cellValues.GetLength(0); i++)
    {
        row = new HtmlTableRow();
        for (int j = 0; j < cellValues.GetLength(1); j++)
        {
            cell = new HtmlTableCell();
            cell.InnerText = cellValues[i, j];
            row.Cells.Add(cell);
        }
        tableContent.Rows.Add(row);
    }

    row = new HtmlTableRow();
    cell = new HtmlTableCell();

    HtmlInputButton input = new HtmlInputButton();
    input.ID = "Button1";
    input.Value = "button";

    cell.ColSpan = 3;
    cell.Controls.Add(input);
    row.Cells.Add(cell);
    tableContent.Rows.Add(row);
}
Sign up to request clarification or add additional context in comments.

Comments

1

I used this code to generate table dynamically in C#.

string connectString = ConfigurationManager.ConnectionStrings["Sample4ConnectionString"].ToString();
        StudentsModelDataContext db = new StudentsModelDataContext(connectString);
        var studentList = db.Students;

        Table tb = new Table();
        tb.BorderWidth = 3;
        tb.BorderStyle = BorderStyle.Solid;
        tb.ID = "myTable";

        foreach (Student student in studentList)
        {
            TableRow tr = new TableRow();

            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            TableCell tc3 = new TableCell();
            TableCell tc4 = new TableCell();
            TableCell tc5 = new TableCell();

            tc1.Text = student.Name;
            tc1.BorderWidth = 2;
            tr.Cells.Add(tc1);

            tc2.Text = student.Email;
            tc2.BorderWidth = 2;
            tr.Cells.Add(tc2);

            tc3.Text = student.Gender;
            tc3.BorderWidth = 2;
            tr.Cells.Add(tc3);

            tc4.Text = student.BirthDate.ToString();
            tc4.BorderWidth = 2;
            tr.Cells.Add(tc4);

            tc5.Text = student.TotalMarks.ToString();
            tc5.BorderWidth = 2;
            tr.Cells.Add(tc5);

            tb.Rows.Add(tr);
        }
        form1.Controls.Add(tb);

This generated the following image. enter image description here

Comments

-1
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim rows As Integer = TextBox1.Text
        Dim columns As Integer = TextBox2.Text

        Dim r As Integer
        Dim c As Integer

        Literal1.Text = "<table border=" & "1" & ">"
        For r = 1 To rows
            Literal1.Text &= "<tr>"
            For c = 1 To columns
                Literal1.Text &= "<td>"
                Literal1.Text &= "&nbsp"
                Literal1.Text &= "</td>"
            Next c
            Literal1.Text &= "</tr>"
        Next r
        Literal1.Text &= "</table>"
    End Sub

1 Comment

Please add an explantaion, why and how does your code will help. You can doing that by cklick on the edil link underneath your post.

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.