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

问题描述:

我试图显示一条错误消息,如果datagridview中的行与我输入的新值匹配。问题是,当我添加一个新的DataGridView有:当dataGridView行匹配文本框时显示错误消息

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

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

下面是代码:

私人无效btnSave_Click(对象发件人,EventArgs的)

{ 
     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

您需要环通的GridView中除了最后一个所有行(这只是现在加入),并比较Cel脱气的'价值每行的l [0]'到'textCode.Text',并且如果你找到了匹配,那么你跳出循环并显示消息。如果循环没有中断,你继续保存。 –

+0

我想我已经接近解决它了。 非常感谢您的回答! 私人无效btnSave_Click(对象发件人,EventArgs的) { 对(INT I = 0; I

+0

您在评论中分享的代码有效吗?你有没有遇到其他问题? –

使用刷新当前单元格的值,

dataGridView.RefreshEdit(); 
dataGridView.Refresh(); 
+0

感谢您的回答!但是......它不起作用...... –