我在DataGridView中进行直接修改,,,当我修改到主键重复的时候,,我进行了判断,,,我是在主键修改之后就判断的...请我判断之后,,如果重复,,,,''''如何使其鼠标返回到原来的位置哦............

解决方案 »

  1.   

    在DataGridView上,,,显示的数据为表A中的 表A的主键为ID,,,,,当我点击修改ID时,,,修改ID后,,我点击另外一个单元格,,他就要进行判断ID是否重复,,,,,,重复的我就想使鼠标又跳到ID这格单元格上哦........................??就是怎样上他跳到原来地单元上哦 .................
      

  2.   

    这是我的解决方法:
      this.dataGridView1.DataError +=
                new DataGridViewDataErrorEventHandler(dataGridView1_DataError);
      private void dataGridView1_DataError(object sender,DataGridViewDataErrorEventArgs e)
     { if (e.Exception != null &&
                    e.Context == DataGridViewDataErrorContexts.Commit)
                {
                    MessageBox.Show("用户ID已存在。");            }
     }
     datagridview 自带一个DataError事件可以判断,但要等到这一行输入完成后,如果你想让一离开单元格就判断的话就要在
      this.dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);中自己验证
     private void dataGridView1_CellValidating(object sender,
                                               DataGridViewCellValidatingEventArgs e)
            {
               ......
            }
      

  3.   

    foreach (DataGridViewRow dgwellrow in dataGridView1.Rows)
                   {
                        foreach (DataGridViewCell cell in dataGridView1.Cells)
                        {
                            // Very important
                            // Let current cell lose focus
                            dataGridView1.CurrentCell = null;                        if (cell.ColumnIndex == 2)
                            {
                                if (判断存在)
                                {
                                    // Select current cell
                                    dataGridView1.CurrentCell = cell;
                                    dataGridView1.BeginEdit(false);
                                    dataGridView1.CurrentRow.ReadOnly = false;
                                    Utility.ShowInformation("你输入的ID号已存在,请核对后重新输入!");
                                    return false;
                                }
                            }
                        }
                    }
      

  4.   

    定义一对int i, j, 用来保存原来的位置, 如果重复, 则dataGridView1.CurrentCell=dataGridView1.Rows[i].Cells[j];