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

急,asp.net中取GridView中某单元格的值总是为空?
前台代码:<asp:GridView ID="gvinfo" runat="server" AutoGenerateColumns="False" 
  Width="680px" AllowPaging="True" 
  onpageindexchanging="gvinfo_PageIndexChanging" EmptyDataText="暂无数据" 
  ShowHeaderWhenEmpty="True" onrowcancelingedit="gvinfo_RowCancelingEdit" 
  onrowdatabound="gvinfo_RowDataBound" onrowediting="gvinfo_RowEditing" 
  onrowupdating="gvinfo_RowUpdating" onrowdeleting="gvinfo_RowDeleting">
  <Columns>
  <asp:BoundField DataField="csName" HeaderText="名称" />
  <asp:BoundField DataField="dY" HeaderText="Y" />
  <asp:BoundField DataField="dX" HeaderText="X" />
  <asp:BoundField DataField="dZ" HeaderText="H" />
  <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
  <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
  </Columns>
  </asp:GridView>
后台代码:点击编辑中的更新时:

 protected void gvinfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
   
  { ControlPoint Point = new ControlPoint();  
  Point.csName = gvinfo.Rows[e.RowIndex].Cells[0].Text.ToString();//这个值是空,但明明是有值的
  Point.dY = double.Parse(gvinfo.Rows[e.RowIndex].Cells[1].Text);//会有异常,提示:输入字符串的格式不正确。我认为是因为取的值是空,所以转换的时候会出错  
  Point.dX = double.Parse(gvinfo.Rows[e.RowIndex].Cells[2].Text);
  Point.dZ = double.Parse(gvinfo.Rows[e.RowIndex].Cells[3].Text);
  PointList[iIndex] = Point;
  this.BindGridView();
   
  }
 

------解决方案--------------------
单步调试看看,到底是不是空!

还有你第一个写的是cells[0],但第二个写的是cells[1].

所以第一个和第二个没关系!
------解决方案--------------------
oint.csName = gvinfo.Rows[e.RowIndex].Cells[0].Text.ToString();//这个值是空,但明明是有值的
这个写法没错,你要去的值是第一个单元格的值嘛?
Point.dY = double.Parse(gvinfo.Rows[e.RowIndex].Cells[1].Text);//会有异常,提示:输入字符串的格式不正确。我认为是因为取的值是空,所以转换的时候会出错
这个是不是有空格或者gvinfo.Rows[e.RowIndex].Cells[1].Text是double的字符串嘛?
改成
Point.dY = double.Parse(gvinfo.Rows[e.RowIndex].Cells[1].Text.Trim());
试试
------解决方案--------------------
用 gvinfo_RowEditing 属性
string csName = gvinfo.Rows[e.NewEditIndex].Cells[0].Text.ToString();//这个值是空,但明明是有值的