我有一些代码可以将新单元格添加到表格中并用文本框填充它们。
到目前为止我的编码方式效果很好:
TableCell tCell1 = new TableCell();
TableCell tCell2 = new TableCell();
TableCell tCell3 = new TableCell();
TableCell tCell4 = new TableCell();
TableCell tCell5 = new TableCell();
TableCell tCell6 = new TableCell();
TableCell tCell7 = new TableCell();
TextBox txt1 = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();
TextBox txt4 = new TextBox();
TextBox txt5 = new TextBox();
TextBox txt6 = new TextBox();
TextBox txt7 = new TextBox();
tCell1.Controls.Add(txt1);
tCell2.Controls.Add(txt2);
tCell3.Controls.Add(txt3);
tCell4.Controls.Add(txt4);
tCell5.Controls.Add(txt5);
tCell6.Controls.Add(txt6);
tCell7.Controls.Add(txt7);
tRow.Cells.Add(tCell1);
tRow.Cells.Add(tCell2);
tRow.Cells.Add(tCell3);
tRow.Cells.Add(tCell4);
tRow.Cells.Add(tCell5);
tRow.Cells.Add(tCell6);
tRow.Cells.Add(tCell7);
正如您所看到的,基本上有 4 条指令重复了 7 次。我确信必须有一种方法可以通过 FOR 循环中的 4 行代码来完成此任务,并动态分配所有名称,但我似乎找不到任何可以为我指明方向的东西做吧。
我所追求的是类似以下的东西:
for (int i = 0; i < 6; i++)
{
TableCell tCell[i] = new TableCell();
TextBox txt[i] = new TextBox();
tCell[i].Controls.Add(txt[i]);
tRow.Cells.Add(tCell[i]);
}
任何帮助将非常感激。
我认为应该这样做:
for (int i = 0; i < 7; i++)
{
TableCell tCell = new TableCell();
TextBox txt = new TextBox();
tCell.Controls.Add(txt);
tRow.Cells.Add(tCell);
}
确保将 6 更改为 7。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)