这是我的cs:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
SqlConnection con = Connection.DBconnection();
if (e.CommandName == "EditRow")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
Textid.Text = gr.Cells[0].Text;
Textusername.Text = gr.Cells[1].Text;
Textclass.Text = gr.Cells[2].Text;
Textsection.Text = gr.Cells[3].Text;
Textaddress.Text = gr.Cells[4].Text;
}
else if (e.CommandName == "Deleterow")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
SqlCommand com = new SqlCommand("StoredProcedure4", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
var id = Int32.Parse(e.CommandArgument.ToString());
GridView1.DeleteRow(id);
com.ExecuteNonQuery();
}
}
我创建了学生详细信息表单,我想删除gridview
中的行。
当我点击删除时,它会显示以下错误:
数据源' SqlDataSource1'不支持删除。除非指定了DeleteCommand。
我是.net的新手。现在开始,
我可以知道,上述代码中的错误是什么?
谢谢,
答案 0 :(得分:0)
else if (e.CommandName == "Deleterow")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
SqlCommand com = new SqlCommand("StoredProcedure4", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
var id = Int32.Parse(e.CommandArgument.ToString());
//comment or remove below line
//GridView1.DeleteRow(id);
com.ExecuteNonQuery();
//rebind the data after delete
GridView1.DataBind();
}