以下为DataGrid的UpdateCommand,我在编辑按钮里面有个更新的按钮,可是在TextBox更新后点'更新',却提示以下错误"异常详细信息: System.InvalidCastException: 指定的转换无效。
源错误: 行 147: //Update the database
行 148:   string computerID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();//DataKeyField 为'computerID'
行 149: string computerName = ((TextBox)e.Item.Cells[0].Controls[0]).Text;
行 150:   string computerUser=((TextBox)e.Item.Cells[1].Controls[0]).Text;
行 151: SqlConnection con=DBOpen.createConnection();
 
"不知道是哪里的错误? private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

//Update the database
  string computerID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();//DataKeyField 为'computerID'
string computerName = ((TextBox)e.Item.Cells[0].Controls[0]).Text;//computerName为第一行第一列
  string computerUser=((TextBox)e.Item.Cells[1].Controls[0]).Text;//computerUser为第二行第二列
SqlConnection con=DBOpen.createConnection();//建立了连接类
con.Open();
SqlCommand cmd=new SqlCommand("UPDATE computerMaintenance " +
"SET computerName = '" + computerName.Replace("'","''")+ "'," +
"computerUser = '" + computerUser.Replace("'","''")+ "'" +
" WHERE computerID = " + e.Item.Cells[0].Text); //数据库名称为computerMaintenance,我要实现更新computerName和computerUser
cmd.ExecuteNonQuery();
this.DataGrid1.EditItemIndex=-1;
this.BindToDataGrid();
}

解决方案 »

  1.   

    int computerID=Convert.ToInt32(this.DataGrid1.DataKeys[e.Item.ItemIndex]);//DataKeyField 为'computerID'
      

  2.   

    string computerName = ((TextBox)e.Item.Cells[0].Controls[0]).Text;//这一行提示错误,问题还是没有得到有效的解决啊!!
      

  3.   

    string computerName = ((TextBox)e.Item.Cells[0].Controls[1]).Text;
      

  4.   

    public void DataGrid1_Update(Object sender, DataGridCommandEventArgs e) 
      {
       TextBox ClassNameText = (TextBox)e.Item.Cells[1].Controls[0];
       string className = ClassNameText.Text;
       int classID = Int32.Parse((e.Item.Cells[0].Text).ToString());
       TextBox orderID2 = (TextBox)e.Item.Cells[5].Controls[0];
       int orderID = Int32.Parse(orderID2.Text);
       ActorClass.ModifyActorClass(className,classID,orderID);   DataGrid1.EditItemIndex = -1;
       if (Request.QueryString.Get("classID") != null)
        Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where parentID=" + Request.QueryString.Get("classID") + " order by depth,orderID desc"));
       else
        Common.BindData(DataGrid1,Common.GetSource("select * from ActorClass where depth=1 order by depth,orderID desc"));
      }
      说明
      (1) DataGrid 事件处理程序的格式
       MethodName(Object sender, DataGridCommandEventArgs e) 
      (2) 更新按钮的说明
       A. 获取编辑状态中的文本框
        TextBox ClassNameText = (TextBox)e.Item.Cells[1].Controls[0];
        string className = ClassNameText.Text;看看别人的代码
      

  5.   

    这个是对datagrid里面的更新列的事件呀。