日期:2014-05-17  浏览次数:20477 次

GridView的一个基础问题
string sqlstr = "update 表 set 
字段1='"+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',
字段2='"+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',
字段3='"+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' 
where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";  

这是一个实现GridView编辑功能的代码,我主要不理解GridView1.Rows[e.RowIndex].Cells[3].Controls[0]的意思。Row代表行对吧?e.RowIndex是不是代表所点击行?还有就是Cells[3]代表第四列对吧?是数据字段的第四列还是说GridView表上的第四列?(我的意思就CommandFiled或者TemplateFiled这些算不算作一列?) Controls[0]又代表什么?
------最佳解决方案--------------------
GridView1.Rows[e.RowIndex].Cells[3].Controls[0]
Rows[e.RowIndex]通过索引访问行
Cell[3],该行上第四个单元格
Controls[0]这个单元格中的第一个控件,比如模板列中可以放多个控件
------其他解决方案--------------------
e.RowIndex 当前选中行的索引
Rows 行   Cells 列
CommandFiled和TemplateFiled 都算一列。
------其他解决方案--------------------
Cells 单元格
Controls 此单元格中的控件集合,Controls[0]就是取得第一个控件
------其他解决方案--------------------
GridView1.Rows[e.RowIndex].Cells[3].Controls[0]

Rows是行   cell是单元格    Controls单元格里面的第几个控件    


因为单元格里面可以放很多控件   

假如你在模版列里面放个TextBox    你想获取TextBox里面的值   就是 Controls[1]