禁止编辑 DataGridView 记录

Blocked from editing DataGridView record

是否可以在阻止重复记录的同时编辑选定的记录?我正在做类似的事情。 Example

当我有很多记录并更新第二条或第三条记录时,每当我尝试使用第一条记录的 ID 时,我都会收到警告;效果很好。但是,每当我尝试编辑第一条记录的名称、城市等时,就会弹出重复 ID 错误,因为我没有更改 ID;它认为自己是重复的。不知道该怎么办。我尝试使用断点,但我没有看到任何有趣的东西。谢谢

private void btnUpdate_Click(object sender, EventArgs e)
    {
        if (dgvProfiles.SelectedCells.Count <= 0)
        {
            MessageBox.Show("No record was selected to update.");
        }

        else {
            for (int row = 0; row < dgvProfiles.Rows.Count; row++)
            {
                for (int col = 0; col < dgvProfiles.Columns.Count; col++)
                {
                    if (dgvProfiles.Rows[row].Cells[col].Value != null &&
                      dgvProfiles.Rows[row].Cells[col].Value.Equals(txtEmail.Text.Trim()))
                    {
                        MessageBox.Show("Duplicate email was entered.");
                        return;
                    }

                    else if (dgvProfiles.Rows[row].Cells[col].Value != null &&
                      dgvProfiles.Rows[row].Cells[col].Value.Equals(txtID.Text.Trim()))
                    {
                        MessageBox.Show("Duplicate ID was entered.");
                        return;

                    }
                }
            }
            DataGridViewRow newDataRow = dgvProfiles.Rows[indexRow];
            newDataRow.Cells[0].Value = txtID.Text;
            newDataRow.Cells[1].Value = txtName.Text;
            newDataRow.Cells[4].Value = txtEmail.Text;
            newDataRow.Cells[5].Value = txtCity.Text;
            newDataRow.Cells[6].Value = cbxState.Text;

        }
    }

正在尝试使用 Validation events

如果您将 DataGridView 绑定到 DataSet,则可以更轻松地遍历 DataSet 中的值以查找重复项。参见 DataSource property