我创建了一个包含一些C#和asp.net的表,用于填充数据库中的数据,但是当我向表中添加数据时,数据将转到同一行。我想把数据放在多行。
我使用的一些代码是:
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++;
}
答案 0 :(得分:2)
移动
TableRow tabelaLinhas = new TableRow();
进入while循环;您只是创建一个行对象,即使您要多次添加。每次迭代都需要创建新实例。
HTH。
答案 1 :(得分:0)
写
tabelaLinhas = new TableRow();
下的
tabela.Rows.Add(tabelaLinhas);