当前位置:编程学习 > C#/ASP.NET >>

datagridview的异常捕捉

使用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#...可能问题有点小白 请大大们不惜赐教 尽量详细些 恩... --------------------编程问答-------------------- 帮顶 蹭分 --------------------编程问答--------------------         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;
                        }
                    }
                }
            }
        }

--------------------编程问答-------------------- 先谢谢LS的回答...尝试了下代码  虽然没有报错但错误依旧


最好还是能用异常捕捉的办法把这个问题解决了 恩.. --------------------编程问答-------------------- 断点看看,以前也碰到过这种错误
--------------------编程问答-------------------- dataGridView1_DataError事件里面不要写内容,就不会出现这个错误了
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,