Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 验证datagridview中的单元格_C#_Winforms_Validation_Datagridview - Fatal编程技术网

C# 验证datagridview中的单元格

C# 验证datagridview中的单元格,c#,winforms,validation,datagridview,C#,Winforms,Validation,Datagridview,我有一个Windows窗体中的DataGridView,它有两列,最后一列将包含一个数据包的名称,重点是这个名称不必重复 我尝试使用CellValidating事件处理此代码 string value = Convert.ToString(dgvTiendas.Rows[e.RowIndex].Cells[e.ColumnIndex].Value); int cellIndex = e.RowIndex; if(cellIndex == 1) {

我有一个Windows窗体中的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;
                    }
                }
            }
    }
当我运行应用程序并写入容器名称时,会对其进行验证,但似乎会验证当前单元格,因为“错误文本”出现在行标题上


请帮助我在几周内完成此操作。

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

dgvTiendas.Rows[e.RowIndex].ErrorText = "Contenedor ya utilizado";
您需要设置单元格的错误文本

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