2

I've created a table with some C# and asp.net for be filled by data from a database, but when I add data to the table, the data is going to the same row. I want to put data in multiple rows.

Some code I have used is:

int tmp = 0;
TableRow tabelaLinhas = new TableRow();
while (tmp < dados.Rows.Count)
{
 TableCell celula = new TableCell();
 celula.Controls.Add(new LiteralControl(dados.Rows[tmp]["nome"].ToString()));
 tabelaLinhas.Cells.Add(celula);
 tabela.Rows.Add(tabelaLinhas);

 CheckBox caixa = new CheckBox();
 caixa.ID = dados.Rows[tmp]["nome"].ToString().Replace(" ", "").ToLower() + "CheckBox";
 celula = new TableCell();
 celula.Controls.Add((Control)caixa);
 tabelaLinhas.Cells.Add(celula);
 tabela.Rows.Add(tabelaLinhas);

 tmp++;
}

2 Answers 2

2

Move

TableRow tabelaLinhas = new TableRow(); 

into the while loop; you are only creating one row object, even though you are adding multiple times. New instances need created in every while iteration.

HTH.

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

Comments

0

write

tabelaLinhas = new TableRow();

under

tabela.Rows.Add(tabelaLinhas);

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.