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

上一篇: 如何从其他课程调用输入对话框(Gorkem Gencay)

下一篇: 当dataGridView行匹配文本框时显示错误消息