vba 将字符串转换为布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26032668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert string to boolean
提问by Marco
I got a datagridview which transfers data to a other datagridview which i would like some help with. It looks like it should be working, however i get a error saying that the string cannot be converted to boolean. This is the case for the checkbox in the datagridview(user has to click the checkboxes of the rows they want to copy.) I know how to convert values to strings etc but i can't seem to get this one figured out. Can anyone help me? This is what happens at the click of the copy button:
我得到了一个 datagridview,它将数据传输到另一个 datagridview,我需要一些帮助。看起来它应该可以正常工作,但是我收到一条错误消息,指出该字符串无法转换为布尔值。这是 datagridview 中复选框的情况(用户必须单击他们想要复制的行的复选框。)我知道如何将值转换为字符串等,但我似乎无法弄清楚这一点。谁能帮我?这是单击复制按钮时发生的情况:
For i As Integer = 0 To DGV1.Rows.Count - 1
If DGV1.Rows(i).Cells(4).Value = True Then
DGV2.Rows.Add(DGV1.Rows(i).Cells(3).Value, _
DGV1.Rows(i).Cells(2).Value, _
DGV1.Rows(i).Cells(3).Value, _
DGV1.Rows(i).Cells(4).Value, _
DGV1.Rows(i).Cells(6).Value)
'2 = voer, 3 =voerid, 4= drogestof, 6 = (voer)keukenlocatie
End If
DGV1 is the datagrid where the data has to come from, so cell4 is the checkbox. DGV2 is the datagrid where the data has to go to.
DGV1 是数据必须来自的数据网格,因此 cell4 是复选框。DGV2 是数据必须转到的数据网格。
Update: Right now i use this code, which got me a bit further.
更新:现在我使用这个代码,这让我更进一步。
Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkopieren.Click
For i As Integer = 0 To DGV1.Rows.Count - 1
If CBool(DGV1.Rows(i).Cells(4).Value) Then
'If DGV1.Rows(i).Cells(4).Value = True Then
DGV2.Rows.Add(DGV1.Rows(i).Cells(0).Value, _
DGV1.Rows(i).Cells(1).Value, _
DGV1.Rows(i).Cells(2).Value, _
DGV1.Rows(i).Cells(3).Value)
'2 = voer, 3 =voerid, 4= drogestof, 6 = (voer)keukenlocatie
End If
Next
End Sub
I get the error "Invalid Operationexception was unhandled" Apparantly, they don't like it when i want to transfer the selected data from 1 datagridview to a other bound datagridview. Is there any solution for this? I tried fixing it myself and googling, but without results.
我收到错误“无效的操作异常未处理”显然,当我想将选定的数据从 1 个 datagridview 传输到另一个绑定的 datagridview 时,他们不喜欢它。有什么解决办法吗?我尝试自己修复并使用谷歌搜索,但没有结果。
回答by prem
Seems like you want to copy the checked rows. So you should the column with the checkbox to validate that it is checked or not. If your first column is of checkbox type then it is Cell(0). Possibly modifying your code as shown below will work for you.
似乎您想复制选中的行。因此,您应该使用带有复选框的列来验证它是否被选中。如果您的第一列是复选框类型,则它是 Cell(0)。如下所示修改您的代码可能对您有用。
For i As Integer = 0 To DGV1.Rows.Count - 1
If CBool(DGV1.Rows(i).Cells(0).Value) Then
DGV2.Rows.Add(DGV1.Rows(i).Cells(3).Value, _
DGV1.Rows(i).Cells(2).Value, _
DGV1.Rows(i).Cells(3).Value, _
DGV1.Rows(i).Cells(4).Value, _
DGV1.Rows(i).Cells(6).Value)
'2 = voer, 3 =voerid, 4= drogestof, 6 = (voer)keukenlocatie
End If
Next
回答by Noodles
Value needs to be true of false string. Maybe a numeric string (0, not 0, -1, not -1), or a number.
值需要为真假字符串。可能是一个数字字符串(0,不是 0,-1,不是 -1),或者是一个数字。
Here's an experiment.
这是一个实验。
A = 0
B = 1
C = -1
D = 55
msgbox A & " " & B & " " & C & " " & D
msgbox cbool(A) & " " & cbool(B) & " " & cbool(C) & " " & cbool(D)
msgbox cbool(cstr(A)) & " " & cbool(cstr(B)) & " " & cbool(cstr(C)) & " " & cbool(cstr(D))
msgbox cbool("true") & " " & cint(vbtrue)
msgbox cbool("false") & " " & cint(vbfalse)
msgbox cbool("cat")
You'll see false = 0 and true <> 0. And cat is an error.
你会看到 false = 0 和 true <> 0。而 cat 是一个错误。