Is it possible to block duplicate records, but also edit a selected record? I am working on something similar to this. Example
When I have numerous records and I update the 2nd or 3rd one, I expect a warning whenever I try to use the first record's ID; that is working nicely. However, whenever I try to edit the first record's name, city, etc, the duplicate ID error pops up, since I did not alter the ID; it is counting itself as a duplicate. No idea what to do. I tried using breakpoints, but I am not seeing anything of interest. Thanks.
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;
}
}
Trying using the Validation events.
And if you bind your DataGridView to a DataSet it will be easier to walk the values in the DataSet to find duplicates. See DataSource property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With