if (dataGridView1.SelectedRows.Count > 0)
{
if (DialogResult.OK == MessageBox.Show("确定要删除?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
int idl = 0;
idl = Int32.Parse(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
userbll.Delete(idl);
this.usertabTableAdapter.Fill(this.sensorDataSet.usertab);
}
}
else
{
MessageBox.Show("请选择一行");
}
这是我编写的butten事件 运行后没有反应
C#datagridview利用主键删除一行 求详细代码
答案:6 悬赏:70
解决时间 2021-01-04 03:20
- 提问者网友:故事与他
- 2021-01-03 14:57
最佳答案
- 二级知识专家网友:许你一世温柔
- 2021-01-03 15:02
public bool DeleteProduct(int productID)
{
bool X;
SqlConnection conn = new SqlConnection
("Data Source=;initial catalog=;user id=sa;pwd =;");
conn.Open();
string delete = "delete from [Products Information] where ProductsID='" + productID + "'";
//Create a connection to DataBase.
SqlCommand cmd1 = new SqlCommand(delete, conn);
int i = cmd1.ExecuteNonQuery();
if (i != 0)
{ X = true; }
else
{
X = false;
}
return X;
}
protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
{
int productID = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("Label1"))).Text);
//Get the ID of the table ProductsInformation.
bool success = DeleteProduct(productID);
if (success)
{
Response.Write("<script>alert('Delete successfully')</script>");
Response.Redirect("AdminHomepage.aspx");
}
else
{
Response.Write("<script>alert('Delete not successfully')</script>");
}
}
{
bool X;
SqlConnection conn = new SqlConnection
("Data Source=;initial catalog=;user id=sa;pwd =;");
conn.Open();
string delete = "delete from [Products Information] where ProductsID='" + productID + "'";
//Create a connection to DataBase.
SqlCommand cmd1 = new SqlCommand(delete, conn);
int i = cmd1.ExecuteNonQuery();
if (i != 0)
{ X = true; }
else
{
X = false;
}
return X;
}
protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
{
int productID = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("Label1"))).Text);
//Get the ID of the table ProductsInformation.
bool success = DeleteProduct(productID);
if (success)
{
Response.Write("<script>alert('Delete successfully')</script>");
Response.Redirect("AdminHomepage.aspx");
}
else
{
Response.Write("<script>alert('Delete not successfully')</script>");
}
}
全部回答
- 1楼网友:蜜罐小熊
- 2021-01-03 20:12
运行了这个事件后
记得重新加载和绑定dataset
貌似是什么databand吧
你试试
不过先确认一下你的this.dataGridView1.CurrentRow.Cells[0].Value.ToString()是否存在值
- 2楼网友:放肆的依賴
- 2021-01-03 19:21
删了之后你要 DataBind啊。 删了之后你不刷新就只能去数据库才能看到有反映咯- -!
- 3楼网友:你好陌生人
- 2021-01-03 18:18
把这句代码
Int32.Parse(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
换成
Int32.Parse(this.dataGridView1.DataKeys.[这里放你要删除的那行的索引].ToString());
你得注意一下DataKeys要在dataGridView属性中加一个主键字段就可以了
给我加分,我回答得挺辛苦!
- 4楼网友:糜废丧逼
- 2021-01-03 17:57
自己加断点一步步调,确认到哪步出问题再看,万一你这事件都没触发呢
- 5楼网友:温柔刺客
- 2021-01-03 16:28
1,gridview中是否有select按钮
2,你要删除行直接在gridview delete事件里写不就行了
3,删除后gridview要重新绑定数据源
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯