不多说,直接来问题:
后台代码:
  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
          {
                
                string 学号 =  GridView1.DataKeys[e.RowIndex].Value.ToString();
                string 课程号 = (GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text.ToString();(出错!指定参数超出有效范围 参数名:index)
                string 成绩 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();
                string 备注 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
                String mys = "server=HCZ-PC;database=xscjgl; User ID=sa;pwd=sa;Trusted_Connection=no";
                SqlConnection mycon1 = new SqlConnection(mys);
                mycon1.Open();
                string sql = "update sc set 成绩='" + 成绩 + "', 备注='" + 备注 + "'where 学号='" + 学号 + "' and 课程号='" + 课程号 + "' ";
                SqlCommand mycom2 = new SqlCommand(sql, mycon1);
                mycom2.ExecuteNonQuery();
                mycom2.Dispose();
                mycon1.Close();
                GridView1.EditIndex = -1;
                bind();           } 
            catch(Exception ee)
         {
             Response.Write("<script>alert('"+ ee.Message+"')</scrip>");
           }
        }
附:在string 学号 =  GridView1.DataKeys[e.RowIndex].Value.ToString();学号有值,但是到课程号的就没有值,我用try ……catch检测不到出错!
各位高手帮帮忙啊!
在网上找了半天的方法,也试了半天就是搞不出来,我是新手!

解决方案 »

  1.   

    中文变量名你的课程号 是GridView中的第五列??string 课程号 = (GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text.ToString();如果是第四列的话,这里的4应该是3,注意索引号是从0开始的。
      

  2.   

    前台代码:
    <asp:GridView ID="GridView1" runat="server"
             
            AutoGenerateColumns="false"
           OnRowCancelingEdit="GridView1_RowCancelingEdit"
    OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" 
      BorderWidth="1px" >
            <Columns>
            <asp:CommandField ShowEditButton="true" />
            <asp:BoundField DataField="学号" HeaderText="学号" ReadOnly="true" />
               
             <asp:BoundField DataField="姓名" HeaderText="姓名" ReadOnly="true" />
                  <asp:BoundField DataField="专业" HeaderText="专业" ReadOnly="true"  />
                  <asp:BoundField DataField="年级" HeaderText="年级" ReadOnly="true" />
              <asp:BoundField DataField="课程号"  HeaderText="课程号"  ReadOnly="true" />
              <asp:BoundField DataField="课程名称" HeaderText="课程名称" ReadOnly="true" />
              <asp:BoundField DataField="开课学期" HeaderText="开课学期" ReadOnly="true" />
              
               <asp:BoundField DataField="成绩" HeaderText="成绩 "/>
                <asp:BoundField DataField="备注" HeaderText="备注"/>
            </Columns>
            
              </asp:GridView>求指点啊 !!!!
      

  3.   

    GridView1.Rows[行索引].Cells[列索引].Text
      

  4.   

     //取得编辑行的关键字段的值
                    string xuehao =  GridView1.DataKeys[e.RowIndex].Value.ToString();
                    //去的文本框中输入的内容
                    string kechenghao = (GridView1.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox).Text.ToString();
                    string chengji = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
                    string beizhu = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString();
      

  5.   

    根据5L,我把代码改成:
    string kechenghao = GridView1.Rows[e.RowIndex].Cells[5].Text.ToString();
                    string chengji = GridView1.Rows[e.RowIndex].Cells[8].Text.ToString();
                    string beizhu =GridView1.Rows[e.RowIndex].Cells[9].Text.ToString();
     这样不存在上面的问题,但是新的问题出来了:
    chengji,beizhu就是读不出来值,其他的列的值都能够读出来,就只有这读不出来。我把除成绩,备注的其他列设置的属性为只读。大侠能解答吗??