如何在选择案例语句中包含数字和字符串
Posted
技术标签:
【中文标题】如何在选择案例语句中包含数字和字符串【英文标题】:How to include a number and string in select case statement 【发布时间】:2013-06-25 23:40:20 【问题描述】:我正在尝试找到一种方法,只允许用户在我的 datagridviewcell 中输入“.32”或“未使用”。现在我的代码如下所示。当我运行代码时,它所做的就是将所有内容更改为“未使用”。即使我输入“.32”,它也会将其更改为“未使用”。有没有什么办法解决这一问题?我感谢任何人可以提供的任何帮助或建议。
If (e.ColumnIndex = 6) Then ' This specifies the column number
Dim cellData = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
MessageBox.Show("Cannot Be Empty") ' This will prevent blank datagridviewcells
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Not Used"
End If
Select Case cellData
Case 0.32
DataGridViewSize.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0.32
Case Else
DataGridViewSize.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "Not Used"
End Select
End If
【问题讨论】:
代码检查为0.32,为什么你输入4时期望它做任何事情? 感谢您的收获!错字 【参考方案1】:我认为没有必要使用选择字符串
最好换成Dim cellData = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
【讨论】:
如果用户输入“.32”以外的任何内容,我仍然希望它显示“未使用” 如果您的 DGV 是数据绑定的,那么您不能将字符串放入数字字段。输入任何数字,例如 0 或 -1,然后使用单元格格式设置值为 0 或 -1 的字段将显示“未使用” " ..以上是关于如何在选择案例语句中包含数字和字符串的主要内容,如果未能解决你的问题,请参考以下文章