110,533
社区成员
发帖
与我相关
我的任务
分享
string strId = string.Empty;
SqlCommand cmd = new SqlCommand(string.Empty, globe.SqlConn);
SqlTransaction myTran = globe.SqlConn.BeginTransaction();
cmd.Transaction = myTran;
try
{
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
strId = dataGridView1["Id", dataGridView1.CurrentCell.RowIndex].Value.ToString();
cmd.CommandText = String.Format("delete from table where Id = '{0}'", strId);
cmd.ExecuteNonQuery();
}
myTran.Commit();
}
catch (Exception err)
{
if (myTran != null)
{
myTran.Rollback();
}
MessageBox.Show("删除失败! " + err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
dataGridView1.SelectedRows[i].Index;
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
}
string strId = dataGridView1["Id", dataGridView1.CurrentCell.RowIndex].Value.ToString();
String strSql = String.Format("delete from table where Id = '{0}'",strId);
SqlCommand cmd = new SqlCommand(strSql, globe.SqlConn);
cmd.ExecuteNonQuery();
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
SendKeys.Send("{DEL}");
private void btnDeleteView_Click(object sender, EventArgs e)
{
if (dgv.SelectedRows.Count == 0)
{
MessageBox.Show("请在数据列表中选择一行数据!","注意!");
return;
}
DataRowView drv = dgv.SelectedRows[0].DataBoundItem as DataRowView;
drv.Delete();//表面上的删除 实际上没有删除
}