日期:2014-05-18  浏览次数:20425 次

取不到gridview单元格里的值
有一个girdview与数据库绑定,test()用来遍历girdview取里面单元格的值,但是循环遍历每次都是空值,gridview的行数和列数可以取到的。请问一下是不是我写错了。很急,在线等。
 if (!IsPostBack)
  {
  this.GridView1.DataSource = ds;
  this.GridView1.DataBind();
  test();
  }






  protected void test()
  {
  for (int i = 0; i < this.GridView1.Rows.Count; i++)
  {
  int a = this.GridView1.Rows[i].Cells.Count;
  string k = this.GridView1.Rows[i].Cells[1].Text.Trim();
  string w = this.GridView1.Rows[i].Cells[0].Text.ToString();
  string t = this.GridView1.Rows[i].Cells[2].Text.ToString();
  }
  }



------解决方案--------------------
这个需要在绑定之后获取,并且是自动绑定列
如果模板列,则需要使用
this.GridView1.Rows[i].FindControl("控件id") 来实现


string k = (this.GridView1.Rows[i].FindControl("label1") as Label).Text.Trim();