日期:2014-05-17  浏览次数:21244 次

删除选中行,总是删除datagridview中第一条
private void btn_Del_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要删除吗?", "删除后不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Current = dataGridView1.CurrentCell.RowIndex; //当前选中行   
                string strID = dataGridView1.Rows[Current].Cells[12].Value.ToString().Trim();
                string strSql = "delete from gps where cstr(编号)=" + strID ;
                OleDbConnection conn = GetConnection();
                try
                {
                    conn.Open();
                    OleDbCommand cmd = new OleDbCommand(strSql, conn);
                    int a = cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
            }
            Admin_Activated(null, null);
        }

------解决方案--------------------
如果是删除后想在dataGridView1中剔除那一行,在执行完删除语句后不需要重新加载,只需要在原有的基础上移除就可以了。  例:dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);      //剔除已删除的项(注:不需重新加载,提高效率) ,可以提高效率