如题。部分代码如下:
private void BindToGridView()
       {
        SqlConnection con = DB.createCon();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = new SqlCommand("select employeeID,LastName,FirstName,title ,birthDate from employees", con);
        DataSet ds = new DataSet();
        sda.Fill(ds, "emp");
        this.GridView1.DataKeys = "employeeID";//错误在此      
        this.GridView1.DataSource = ds.Tables["emp"];
        this.GridView1.DataBind();
       }
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string empID = this.GridView1.DataKeys [e.RowIndex].ToString();
        SqlConnection con = DB.createCon();
        SqlCommand cmd = new SqlCommand("delete *  from employees where employeeID='" + empID + "'", con);
        con.Open();
        cmd.ExecuteNonQuery();
        this.BindToGridView();
    }
错误 1 无法对属性或索引器“System.Web.UI.WebControls.GridView.DataKeys”赋值 -- 它是只读的
错误 2 无法将类型“string”隐式转换为“System.Web.UI.WebControls.DataKeyArray”

解决方案 »

  1.   


            this.GridView1.DataKeys = new string[]{"employeeID"};
      

  2.   

    恩谢谢了,综合你们讲的 设置 为this.GridView1.DataKeyNames = new string[] { "employeeID" };
    但是还是报错。  cmd.ExecuteNonQuery();  将 varchar 值 'System.Web.UI.WebControls.DataKey' 转换为数据类型为 int 的列时发生语法错误。
    帮忙看一下怎么回事》
      

  3.   

    我用断点执行了一下,string empID = this.GridView1.DataKeys [e.RowIndex].ToString(); 这一句取不出employeeID的值。
    怎么回事,快帮帮忙,在线等。
      

  4.   

    "delete *  from employees where employeeID="+ empID
      

  5.   

    string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcon = new SqlConnection(strCon);
            sqlcom = new SqlCommand(sqlstr,sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            bind();