show your code, or try Table t = new Table(); TableRow tr = new TableRow(); TableCell tc = new TableCell(); tc.Text = "abc"; tr.Cells.Add(tc); t.Rows.Add(tr); Response.Write("***" + t.Rows[0].Cells[0].Text + "***");
tbResult.Rows[0].Cells[0].Controls有多少?
saucer(思归/MVP), 我单独在一个页面照你这么写可以获取text的值, 可是在我自己的页面就不行,区别就是我定义了一个全局变量 Table tbResult; 然后在不同的函数中操作tbResult这个对象,以下是我主要的程序 private void ConstructTable(ref string refQuerySQL) { TableRow rowResult = new TableRow(); string strTmp="\\NULL//",strTmp2=""; OleDbCommand commSQL = new OleDbCommand(refQuerySQL,dbConnection); OleDbDataReader rdEgood = commSQL.ExecuteReader(); while(rdEgood.Read()) { strTmp2 = rdEgood[1].ToString().Trim(); if(strTmp!=strTmp2)//insert a row { tbResult.Rows.Add(rowResult); rowResult = new TableRow(); nAltermate = (++nAltermate%2); if(nAltermate==1) rowResult.BackColor = Color.AliceBlue; else rowResult.BackColor = Color.White; InsertCell("LightGray",rdEgood[4].ToString().Trim(),ref rowResult); InsertCell("LightGray",rdEgood[5].ToString().Trim(),ref rowResult); InsertCell("LightGray","",ref rowResult); InsertCell("LightGray",rdEgood[6].ToString().Trim(),ref rowResult); InsertCell("LightGray",rdEgood[1].ToString().Trim(),ref rowResult); InsertCell("LightGray",rdEgood[2].ToString().Trim(),ref rowResult); InsertCell("LightGray","",ref rowResult); for(int i=7;i<arrHead.Count;i++) //fill with space for each cell InsertCell("","",ref rowResult); strTmp = strTmp2; } for(int i=7;i<arrHead.Count;i++)//update cell(amount) to fit position { if(arrHead[i].ToString().Trim() == rdEgood[3].ToString().Trim()) { rowResult.Cells[i].Text = rdEgood[0].ToString().Trim(); rowResult.Cells[i].ToolTip = rdEgood[2].ToString().Trim()+"(机型:"+arrHead[i].ToString().Trim()+")"; rowResult.Cells[i].Wrap = false; } } } tbResult.Rows.Add(rowResult); rdEgood.Close(); commSQL.Dispose(); }
rowResult.Cells[i].Text = rdEgood[0].ToString().Trim();现在我想取cell的值
String temp = tbResult.Rows[0].Cells[1].Text;可是取出来都是空的,可是我输出tbResult的行数是正确的,说明tbResult
已经构造和赋值好了呀!
TableRow rowResult = tbResult.Rows[0];
上面的写法是错误的!
这样不行,取出来是空的,
tbResult.Rows[0].Cells[1].Text = "ddddddd";
这样就可以,可以设置单元格的值。
可是我看帮助说Text是设置和获取Cell的值呀?
你说的是增加第一行,没有问题,我现在是已经有了第一行,
可以更改第一行每一个Cell的值,但是就是没法获取它的值,
为什么?
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Text = "abc";
tr.Cells.Add(tc);
t.Rows.Add(tr);
Response.Write("***" + t.Rows[0].Cells[0].Text + "***");
我单独在一个页面照你这么写可以获取text的值,
可是在我自己的页面就不行,区别就是我定义了一个全局变量
Table tbResult;
然后在不同的函数中操作tbResult这个对象,以下是我主要的程序
private void ConstructTable(ref string refQuerySQL)
{
TableRow rowResult = new TableRow();
string strTmp="\\NULL//",strTmp2="";
OleDbCommand commSQL = new OleDbCommand(refQuerySQL,dbConnection);
OleDbDataReader rdEgood = commSQL.ExecuteReader();
while(rdEgood.Read())
{
strTmp2 = rdEgood[1].ToString().Trim();
if(strTmp!=strTmp2)//insert a row
{
tbResult.Rows.Add(rowResult);
rowResult = new TableRow();
nAltermate = (++nAltermate%2);
if(nAltermate==1)
rowResult.BackColor = Color.AliceBlue;
else
rowResult.BackColor = Color.White;
InsertCell("LightGray",rdEgood[4].ToString().Trim(),ref rowResult);
InsertCell("LightGray",rdEgood[5].ToString().Trim(),ref rowResult);
InsertCell("LightGray","",ref rowResult);
InsertCell("LightGray",rdEgood[6].ToString().Trim(),ref rowResult);
InsertCell("LightGray",rdEgood[1].ToString().Trim(),ref rowResult);
InsertCell("LightGray",rdEgood[2].ToString().Trim(),ref rowResult);
InsertCell("LightGray","",ref rowResult);
for(int i=7;i<arrHead.Count;i++) //fill with space for each cell
InsertCell("","",ref rowResult);
strTmp = strTmp2;
}
for(int i=7;i<arrHead.Count;i++)//update cell(amount) to fit position
{
if(arrHead[i].ToString().Trim() == rdEgood[3].ToString().Trim())
{
rowResult.Cells[i].Text = rdEgood[0].ToString().Trim();
rowResult.Cells[i].ToolTip = rdEgood[2].ToString().Trim()+"(机型:"+arrHead[i].ToString().Trim()+")";
rowResult.Cells[i].Wrap = false;
}
}
}
tbResult.Rows.Add(rowResult);
rdEgood.Close();
commSQL.Dispose();
}
但是就是取不出来,不明白为什么?