0
现在我有一个vb.net程序读取excel文件并将内容显示到datagridview中。我的目标是让代码告诉用户单元中除1或空白之外是否有任何值。然后我想为datagridviewcell提供一个空白的默认值。如果默认值是1,我很容易做到这一切,但是当我将其更改为空白(String.empty)时,程序将保留无效输入。以下是我的代码如下。如果任何人可以弄清楚如何让程序提供一个空值作为默认值,我将非常感激! :)如何使datagridviewcell的默认值为空
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
你有没有尝试传递一个毫无作为的价值? .Cells(columnindex).Value = Nothing – Jaxedin
感谢您的建议,是的,我试过“没有”,它仍然没有为我工作 – stackexchange12
我试试这个:添加.ToString填充CellData行并调整字符串值的代码。 Dim cellData作为字符串。 – rheitzman