如题。部分代码如下:
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”
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”
this.GridView1.DataKeys = new string[]{"employeeID"};
但是还是报错。 cmd.ExecuteNonQuery(); 将 varchar 值 'System.Web.UI.WebControls.DataKey' 转换为数据类型为 int 的列时发生语法错误。
帮忙看一下怎么回事》
怎么回事,快帮帮忙,在线等。
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();