防止DataGridView某一列重复输入相同的值

来源:互联网 发布:sqlserver 编辑:程序博客网 时间:2024/05/23 19:20

 

 

                string sSQL = "SELECT P.Part_NO,u.Unit_Code,u.Unit_Name,p.Unit_Rate FROM PARTUNITRATE P,UNITFLM U WHERE P.UNIT_CODE=U.Unit_Code and p.part_no='{0}'";
                ServiceSysmBLL.SysmBLL sysmBLL = new ServiceSysmBLL.SysmBLL();
                DataSet ds=sysmBLL.RunSQLQuery_RunSQL(string.Format (sSQL, Key));
                DataColumn[] keyObj = new DataColumn[1];
                keyObj[0] = ds.Tables[0].Columns["Unit_Code"];
                ds.Tables[0].PrimaryKey = keyObj;

                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = ds.Tables[0].TableName;

错误信息提示:

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
        {
            if (anError.Context == DataGridViewDataErrorContexts.Commit)
            {
               
            }
            if ((anError.Exception) is ConstraintException)
            {
                MsgErr("您好,单位不能重复设置!", m_FormID);
                anError.ThrowException = false;
            }

        }

原创粉丝点击