2013-09-27 65 views
1

我在Windows窗体中有一个DataGridView,它有2列,最后一列将包含一个数据包的名称,重点是这个名称永远不必重复。验证datagridview中的单元格

我使用CellValidating事件

 string value = Convert.ToString(dgvTiendas.Rows[e.RowIndex].Cells[e.ColumnIndex].Value); 
     int cellIndex = e.RowIndex; 

    if(cellIndex == 1) 
    { 
      foreach (DataGridViewRow rw in dgvTiendas.Rows) 
      { 
       if (cellIndex == rw.Index) 
       { 
        return; 
       } 
       else 
       { 
        string valorRow = Convert.ToString(rw.Cells["ContenedorCodigoBarras"].Value); 
        if (value == valorRow) 
        { 
         MessageBox.Show("The container is already used"); 
         dgvTiendas.Rows[e.RowIndex].ErrorText = "Contenedor ya utilizado"; 
         e.Cancel = true; 
        } 
        else 
        { 
         e.Cancel = false; 
        } 
       } 
      } 
    } 

当我运行的应用程序,我写一个容器的名称与此代码尝试,它是有效的,但它似乎验证了当前单元格,因为出现在“错误文本” RowHeader。

请帮助我有几个星期了。

回答

0

您遇到此问题是因为您正在使用此代码设置该行的错误文本。

dgvTiendas.Rows[e.RowIndex].ErrorText = "Contenedor ya utilizado"; 

您需要设置单元格的错误文本

dgvTiendas[e.ColumnIndex, e.RowIndex].ErrorText = "an error occured";