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。
请帮助我有几个星期了。