Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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
Vb.net 如何使datagridviewcell的默认值为空_Vb.net_Validation_Datagridview - Fatal编程技术网

Vb.net 如何使datagridviewcell的默认值为空

Vb.net 如何使datagridviewcell的默认值为空,vb.net,validation,datagridview,Vb.net,Validation,Datagridview,现在我有一个vb.net程序,它读取excel文件并将内容显示到datagridview中。我的目标是让代码告诉用户单元格中是否有除1或空白以外的值。然后我想为datagridviewcell提供一个默认值blank。如果默认值为1,我可以轻松完成所有这些操作,但当我将其更改为空(String.empty)时,程序将保留无效输入。下面是我的代码。如果有人能想出如何让程序提供一个空白值作为默认值,我将不胜感激!:) Sub-validateDGV(行索引、列索引) 作为字符串的Dim值=DataG

现在我有一个vb.net程序,它读取excel文件并将内容显示到datagridview中。我的目标是让代码告诉用户单元格中是否有除1或空白以外的值。然后我想为datagridviewcell提供一个默认值blank。如果默认值为1,我可以轻松完成所有这些操作,但当我将其更改为空(String.empty)时,程序将保留无效输入。下面是我的代码。如果有人能想出如何让程序提供一个空白值作为默认值,我将不胜感激!:)

Sub-validateDGV(行索引、列索引)
作为字符串的Dim值=DataGridView1.Rows(rowindex).Cells(columnindex).value.ToString
如果(columnindex=1),则
Dim cellData=DataGridView1.Rows(rowindex).Cells(columnindex).Value
如果cellData为Nothing或LSE IsDBNull(cellData)或LSE cellData.ToString=String.Empty,则
“什么都不做,因为这是允许的
ElseIf cellData 1则
MessageBox.Show(“值必须为1或空”)
DataGridView1.Rows(rowindex).Cells(columnindex).Value=String.Empty'这应该为我的datagridview提供默认值blank,但不是:(
出口接头
如果结束
如果结束
端接头

由于我使用OleDB,我必须将cellData变量设置为“DBNull.Value”而不是String.Empty

是否尝试将nothing作为值传递?.Cells(columnindex).Value=nothing感谢您的建议,是的,我尝试了“nothing”它仍然不适用于meI。我可以尝试以下操作:在填充CellData的行中添加.ToString,并调整字符串值的代码。将CellData设置为字符串。注意,您可以使用网格的CellFormatting事件更改字体/背景色或设置ErrorProvider文本。
Sub validateDGV(rowindex, columnindex)

    Dim value As String = DataGridView1.Rows(rowindex).Cells(columnindex).Value.ToString
    If (columnindex = 1) Then
        Dim cellData = DataGridView1.Rows(rowindex).Cells(columnindex).Value
        If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
            'Do nothing because this is allowed
        ElseIf cellData <> 1 Then
            MessageBox.Show("Value must be 1 or Blank")
            DataGridView1.Rows(rowindex).Cells(columnindex).Value = String.Empty 'This should be supplying the default value of blank back to my datagridview but its not :(
            Exit Sub

        End If
    End If
End Sub