使用datagridview进行数据输入时....因为表中主键所在列的唯一性约束  当两行数据的列项值相同时……会弹出datagridview默认错误对话框本来想用try…catch来捕获异常 可是不知道相关事件及具体写法默认对话框提示使用dataerror事件来重写错误信息本人查阅MSDN后使用以下代码仍然不能触发事件...求解决方法  private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            // If the data source raises an exception when a cell value is 
            // commited, display an error message.
            if (e.Exception != null &&
                e.Context == DataGridViewDataErrorContexts.Commit)
            {
                MessageBox.Show("CustomerID value must be unique.");
            }
        }本人初接触C#...可能问题有点小白 请大大们不惜赐教 尽量详细些 恩...

解决方案 »

  1.   

            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                int count = this.dataGridView1.Rows.Count;
                if (count > 1 && dataGridView1.CurrentCell.RowIndex>0)
                {
                    if (this.dataGridView1.CurrentCell.ColumnIndex == 1)
                    {
                        for (int i = 0; i <this .dataGridView1.CurrentCell .RowIndex ; i++)
                        {
                            if (this.dataGridView1.Rows[i].Cells[1].Value.ToString() == this.dataGridView1.Rows [this .dataGridView1 .CurrentCell .RowIndex ].Cells [1].Value .ToString ())
                            {
                                MessageBox.Show("此值重复","提示");
                                break;
                            }
                        }
                    }
                }
            }
      

  2.   

    dataGridView1_DataError事件里面不要写内容,就不会出现这个错误了