Display Error Message when dataGridView Row match textbox
I'm trying to display an error message, if the rows in the datagridview match the new value I entered in . The problem is that after I add a new datagridview with:
Product c = new Product();
tableProducts.Products.Add(c);
productBindingSource.Add(c);
productBindingSource.MoveLast();
A new row is created, and when I want to save it, it compares this row code with that of the textbox and causes the error message to be displayed. But it should only be compared with those already stored in the table, not the one I add as new:
Here is the code:
private void btnSave_Click(object sender, EventArgs e)
{
if (dataGridView.CurrentRow.Cells[0].Value.ToString() == txtCode.Text && txtCode.Enabled == true)
{
MessageBox.Show("Code already exist! Try another one!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
panel.Enabled = false;
defaultViewButtons();
}
else
{
productoBindingSource.EndEdit();
tablaProductos.SaveChangesAsync();
panel.Enabled = false;
defaultViewButtons();
}
}
在下面使用刷新当前单元格的值,
dataGridView.RefreshEdit();
dataGridView.Refresh();
链接地址: http://www.djcxy.com/p/71170.html