首页 文章

dataGridView行匹配文本框时显示错误消息

提问于
浏览
2

我正在尝试显示错误消息,如果datagridview中的行与我输入的新值匹配 . 问题是我添加一个新的datagridview后:

Product c = new Product();
            tableProducts.Products.Add(c);
            productBindingSource.Add(c);
            productBindingSource.MoveLast();

创建一个新行,当我想保存它时,它会将此行代码与文本框的行代码进行比较,并导致显示错误消息 . 但它应该只与已经存储在表中的那些进行比较,而不是我添加为新的那个:

这是代码:

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();
        }
    }

1 回答

  • -1

    使用下面的内容刷新当前单元格的值,

    dataGridView.RefreshEdit();
    dataGridView.Refresh();
    

相关问题