首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

一道关于GridView中RowDeleting的有关问题

2012-01-10 
一道关于GridView中RowDeleting的问题代码如下:protectedvoidGridView2_RowUpdating(objectsender,GridVie

一道关于GridView中RowDeleting的问题
代码如下:    

protected   void   GridView2_RowUpdating(object   sender,   GridViewUpdateEventArgs   e)
        {
                   
                  string   sqlstrUD   =   "update   xsxx   set   ";
                sqlstrUD   += "xm=   ' "   +   ((TextBox)GridView2.Rows[e.RowIndex].Cells[2].Controls[0]).Text   +   " ', ";
                sqlstrUD   +=   "xb= ' "   +   ((TextBox)GridView2.Rows[e.RowIndex].Cells[3].Controls[0]).Text   +     " ', ";
                sqlstrUD   += "pwd= ' "   +   ((TextBox)GridView2.Rows[e.RowIndex].Cells[4].Controls[0]).Text   + " ', ";
                sqlstrUD   += "bjmc= ' "   +   ((TextBox)GridView2.Rows[e.RowIndex].Cells[5].Controls[0]).Text   + " ', ";
                sqlstrUD   += "zymc= ' "   +   ((TextBox)GridView2.Rows[e.RowIndex].Cells[6].Controls[0]).Text   + " ', ";  
                sqlstrUD   += "xxzydh= ' "   +   ((TextBox)GridView2.Rows[e.RowIndex].Cells[7].Controls[0]).Text   + " ' ";
                  sqlstrUD   +=   "   where   xh=   ' "   +   ((TextBox)GridView2.Rows[e.RowIndex].Cells[1].Controls[0]).Text   +   " ' ";
                 
              bool   test=     da.ExceSQL(sqlstrUD);         //调用方法执行SQL语句
              if(test==true)
              {
                Response.Write( "更新成功 ");
             
              }    
              else
              {
              Response.Write( "更新失败 ");
              }
              GridView2.EditIndex   =   -1;
              dlBind();
                   
  }
       
以上对数据库的更新完全可以,而下面的删除代码出了问题

        protected   void   GridView2_RowDeleting(object   sender,   GridViewDeleteEventArgs   e)
        {
          string   sqlstrDT= "delete   from   xsxx   where   xh= ";
            sqlstrDT   +=   "   ' "   +((TextBox)GridView2.Rows[e.RowIndex].Cells[1].Controls[0]).Text+ " ' ";
          bool   test   =   da.ExceSQL(sqlstrDT);
          if   (test   ==   true)


          {
                  Response.Write( "删除操作成功! ");
                  GridView2.EditIndex   =   -1;
                  dlBind();


          }
          else
          {
                  Response.Write( "删除失败! ");
          }
       

        }
错误显示行“sqlstrDT   +=   "   ' "   +((TextBox)GridView2.Rows[e.RowIndex].Cells[1].Controls[0]).Text+ " ' ";”
错误信息:指定的参数已超出有效值的范围。参数名:   index  
其中   数据库中xh的类型为char   要删除的值中   xh= '012006026531 '
 

希望网友抽空帮忙解决一下,   使其能正常删除,谢谢啦!!




[解决办法]
跟踪一下。Cells[1]中是否有控件。
[解决办法]
所以报错
[解决办法]
单元格位置不对

等你学会了用模版列去FindControl就再也不会用Cells[X].Controls[Y]这种容易出错的语法了

热点排行