奇怪!这两种asp.net代码 为什么 执行结果一样??

来源:百度知道 编辑:UC知道 时间:2024/05/22 19:32:38
1、第一段代码:softGV是个aspx网页上的gridview控件

for (int i = 0; i < softGV.Rows.Count; i++)
{
TableCell tc = softGV.Rows[i].Cells[1];
tc.Visible = false; //隐藏所有行中的第2列数据

}

运行结果证实,上面代码的确可以 将softGV的每一行的第二列隐藏,我奇怪的是:tc.Visible = false操作的对象是tc,这不同于softGV.Rows[i].Cells[1].Visible = false; 按说上述代码不应该具有列的 隐藏效果的呀!
可是为什么却能够隐藏列呢??
感谢各位的回答,但是我的疑惑没有解决,我举个例子:

在c语言中:
int a=10;
int x=a;
x=20;
显然,x的值变为20 ,a的值还是10,同理:
{
TableCell tc = softGV.Rows[i].Cells[1];
tc.Visible = false;
}
和softGV.Rows[i].Cells[1].Visible怎么会一样呢??

TableCell tc = softGV.Rows[i].Cells[1];
tc.Visible = false; //隐藏所有行中的第2列数据

softGV.Rows[i].Cells[1].Visible = false;

这两个隐藏的有什么不同么?
我觉得应该一样啊。
都是隐藏了GridView 的Cell

为什么一样?
因为x你设的是int 是给x一个值类型
而 softGV.Rows[i].Cells[1]是一个TableCell

TableCell tc = softGV.Rows[i].Cells[1];
这句是实例化出来一个TableCell 叫tc.
等号的作用就是
让tc这个对象 是softGV.Rows[i].Cells[1]
这两个过程是不等同的。

是一样的,在这里你是定义了一个TableCell 类型变量tc ,并初始化tc为 softGV.Rows[i].Cells[1],也就是说tc=softGV.Rows[i].Cells[1],所以tc.Visible = false,自然是隐藏每一行的第二列。
ps:
TableCell tc = softGV.Rows[i].Cells[1];
你应该是这句没搞懂,这句等同于
TableCell tc;
tc = softGV.Rows[i].Cells[1];
要干IT行业就要研究到底,从理论学知识,到实践,加油,你行的···

tc = softGV.Rows[i].Cells[1]
tc.Visible = softGV.Rows[i].Cells[1].Visible
为啥不一样呢

为什么这样喜欢科研,能正常运行就OK了.