解决方案 »

  1.   

            private void dgvData3_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
            {//右键选中dgvData3当前行当前项
                if (e.Button == MouseButtons.Right)
                {
                    dgvData3.ClearSelection();
                    dgvData3.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
                    dgvData3.CurrentCell = dgvData3.Rows[e.RowIndex].Cells[e.ColumnIndex];
                }
            }
            
            private void contextMenuStrip3_Opening(object sender, CancelEventArgs e)
            {
                startPosition = Cursor.Position;
            }        private void toolStripMenuItem6_Click(object sender, EventArgs e)
            {
                DataGridView.HitTestInfo ti;
                Point p = dgvData3.PointToClient(startPosition); //坐标转换
                ti = dgvData3.HitTest(p.X, p.Y);
                try
                {
                    if (dgvData3.Rows.Count <= 0) return;
                    string descr = dgvData3.Rows[ti.RowIndex].Cells["Descr"].Value.ToString();
                    
                    string alarmID = dgvData3.Rows[ti.RowIndex].Cells["ID"].Value.ToString();
                    string sql = "update DT_AlarmData ";
                    sql += " set Valid = '0' ";
                    sql += " where ID = '" + alarmID + "'";
                    Define.Database.ExecuteNonQuery(sql, null);
                    
                    LoadData();
                }
                catch (Exception ex)
                {
                }
            }
    **************************************************
     public void LoadData()
            {
                //装载数据
                try
                {
                    string sql = "select DT_Area.ID, DT_Area.AName";
                    sql += ",isnull((select SUM(MNP + ANP) from DT_Park where AID=DT_Area.ID),0) as SMNP";
                    sql += ",isnull((select SUM(CNP) from DT_Park where AID=DT_Area.ID),0) as SCNP from DT_Area";
                    DataTable table = Define.Database.ExecuteDataTable(sql, null);
                    Define.AddGridRow(dgvData1, table, _Fields1, 0);                sql = "select ID, AType, ATime, Descr from DT_AlarmData ";
                    sql += " where Valid = '1' ";
                    table = Define.Database.ExecuteDataTable(sql, null);
                    pageControl2.SourceData = table;

                }
                catch (Exception ex)
                {
                    Public.Dialog.Common.ShowDialog("装载数据时出错:" + ex.Message, "错误", MessageBoxIcon.Error);
                }
            }
      

  2.   

    修改 之后 让 datagridView 失去焦点,你没有结束它的编辑状态。