各位大哥,求在datagridview中删除选中行,并删除数据库中数据的代码!

谢谢了!c#.net做winform,使用一个删除按钮!

简单:
下面是按键下代码,你自己改!
SqlConnection _con = new SqlConnection(@"Data Source=192.168.0.34\WTMT;Initial Catalog=WtmtDatabase;User ID=sa;pwd=wtmt");
SqlCommand _cmd;
try
{
if (this.dataGridView1.Focused)
{
string strName = this.dataGridView1.CurrentRow.Cells["mp_LotNumber"].Value.ToString().Trim();

try
{
DialogResult dr = MessageBox.Show(this, "您确定要删除 : <" + strName + "> 该跟单信息吗?", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
int rowCount = 0; //存储受影响行数
string sqlText = "delete from mainProcess where mp_LotNumber = '" + strName + "'";
_con.Open();
_cmd = new SqlCommand(sqlText , _con);
rowCount = _cmd.ExecuteNonQuery();
if(rowCount != 0)
{
MessageBox.Show("删除成功!");
_cmd.close();
_con.close();
}
else
{
_cmd.close();
_con.close();
MessageBox.Show("删除失败!");
}
}

}
catch (Exception se)
{
MessageBox.Show(se.Message);
}

}
else
{
MessageBox.Show("请您在跟单信息报表中选择您要删除的跟单名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch (Exception)
{
MessageBox.Show("所选数据为空!请先选择数据再操作!");
}

有疑问可以直接HI我
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-21
/// <summary>
/// 删除
/// </summary>
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
#region Shift多行删除
if (this.dataGridView1.SelectedRows.Count > 0)
{
if (MessageBox.Show("确定要删除选中的员工信息吗?删除后将不可恢复!", "小鱼提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Del();
}

}
else
{
MessageBox.Show("请选择要删除的员工信息!", "小鱼提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
#endregion
}
/// <summary>
/// 删除的方法
/// </summary>
public void Del()
{
int k = this.dataGridView1.SelectedRows.Count;
if (dataGridView1.Rows.Count > 0)
{
for (int i = k; i >= 1; i--)//从下往上删,避免沙漏效应
{
string usercode = dataGridView1.SelectedRows[i - 1].Cells["UCode"].Value.ToString();
if(userbll.DelUser(usercode))
{
}
else
{
MessageBox.Show("删除失败!", "小鱼提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
this.dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i - 1].Index);
}
}
else
{
dataGridView1.Rows.Clear();
}

}
第2个回答  2010-04-21
看不到你的代码,只能给你个思路,删除行的时候得到当前的数据ID(前提是绑定的时候绑定ID 可以隐藏掉)执行SQL语句即可
第3个回答  2010-04-28
//删除操作
private void deleteBtn_Click(object sender, EventArgs e)
{
try
{
if (this.customerGridView.Rows.Count <= 0)
{
MessageBox.Show("没有要删除的内容");
}
else
{
DialogResult result = MessageBox.Show("确认删除此条记录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{

string delete_No = this.customerGridView.SelectedRows[0].Cells["客户编号"].Value.ToString();
string sql_str_delete = "delete from customerInfo where 客户编号='" + delete_No + "'";
SqlConnection conn = dbManage.getcon();
SqlCommand com = new SqlCommand(sql_str_delete, conn);
int flag = dbManage.ExeSQL(sql_str_delete);

if (flag == 0)
{
this.customerGridView.Rows.RemoveAt(this.customerGridView.SelectedRows[0].Index);
MessageBox.Show("记录已成功删除", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
}