Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3218552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:32:28+00:00 2026-05-17T15:32:28+00:00

Simplified Question: I want to use the following to build an html table dynamically.

  • 0

Simplified Question:

I want to use the following to build an html table dynamically.

   //table.Rows[row].Cells.AddRange(new TableCell[] { new TableCell(), new TableCell(), new TableCell(), new TableCell(), new TableCell(), new TableCell() });


for (int row = 0; row < intRows; row++)
{
    table.Rows.Add(new TableRow());
    table.Rows[row].Cells.AddRange(new TableCell[intCellsPerRow]);
    intTableRows = row;

}

I used the commented line before, but it is not flexible, so is not what I want.

The Line: table.Rows[row].Cells.AddRange(new TableCell[intCellsPerRow]); does not work.

How can I get this to work?

Answer Thanks to Heinzi

  for (int row = 0; row < dtStructure.Rows.Count / TABLE_COLUMNS; row++)
            {

                table.Rows.Add(new TableRow());

                for (int i = 0; i < CELLS_PER_COLUMN * TABLE_COLUMNS; i++)
                {
                    table.Rows[row].Cells.Add(new TableCell());
                }

                intTableRows = row;
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:32:29+00:00Added an answer on May 17, 2026 at 3:32 pm

    You are adding the same cells to all the rows. This is what happens: After the first iteration, the (CELLS_PER_COLUMN * TABLE_COLUMNS) cells you created have the first row as their parent. After the second iteration, their parent is changed to the second row, etc.. In the end, they all end up in the last row. Note that ToArray does not copy the cells, it just copies the references to the cells into a new array. So all the rows try to share the same cells, which does not work (a WebControl such as TableCell can only have a single parent).

    For every row, you need to create new cells. I assume that you want something like this:

    for (int row = 0; row < datatable.Rows.Count / 6; row++)
    {
        table.Rows.Add(new TableRow());
        for (int i = 0; i < CELLS_PER_COLUMN * TABLE_COLUMNS; i++)
        {
            table.Rows[row].Cells.Add(new TableCell());
        }
    }
    

    Untested, since I don’t have Visual Studio available right now, but you should get the idea…

    EDIT: I just saw that you edited your question. Your line

    table.Rows[row].Cells.AddRange(new TableCell[intCellsPerRow]);
    

    does not work, because here you add an empty array of intCellsPerRow table cells, i.e., your array contains {null, null, ...}. You need to create each cell with new like in my code example above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My simplified and contrived example is the following:- Lets say that I want to
I have the following worksheet #1 (simplified for the question) of person and age:
I have the following methods in an enum helper class (I have simplified it
I have a piece of Perl code somewhat like the following (strongly simplified): There
Simplified for example, I have two tables, groups and items . items ( id,
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Here's a simplified version of what I'm trying to do : Before any other
Here is a simplified version of my database model. I have two tables: Image,
Below is my (simplified) schema (in MySQL ver. 5.0.51b) and my strategy for updating
Here is a simplified version of my application showing what I'm doing. /* in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.