动态生成一个3行3列的表格,在设置表格单元格宽度的时候出问题了,报错:CS0019: 运算符“/”无法应用于“System.Web.UI.WebControls.Unit”和“int”类型的操作数。 请高手指点。 具体代码如下: Table1.Width = 390;
for (int i = 0; i < 3; i++) {
TableRow myRow = new TableRow();
for (int j = 0; j < 3; j++) {
TableCell myCell = new TableCell();
myCell.Width = Table1.Width / 3;
myRow.Cells.Add(myCell);
}
Table1.Rows.Add(myRow);
}
for (int i = 0; i < 3; i++) {
TableRow myRow = new TableRow();
for (int j = 0; j < 3; j++) {
TableCell myCell = new TableCell();
myCell.Width = Table1.Width / 3;
myRow.Cells.Add(myCell);
}
Table1.Rows.Add(myRow);
}
异常详细信息: System.InvalidCastException: 无法将类型为“System.Web.UI.WebControls.Unit”的对象强制转换为类型“System.IConvertible”。
protected void Page_Load(object sender, EventArgs e)
{
Table Table1 = new Table();
Table1.Width = 390;
for (int i = 0; i < 3; i++)
{
TableRow myRow = new TableRow();
for (int j = 0; j < 3; j++)
{
TableCell myCell = new TableCell();
myCell.Width = new Unit(Table1.Width.Value / 3);
myRow.Cells.Add(myCell);
}
Table1.Rows.Add(myRow);
}
Page.Controls.Add(Table1);
}
思路正确,但要写成:
myCell.Width = Table1.Width / (uint)3;