我正在构建一个充满购物等项目的应用程序,并且用户(在登录并选择“浏览类别”之后)然后可以看到列表框和按钮(以及返回的按钮)。列表框具有类别名称,对于每个类别,都有一个表单,用于保存该类别下的项目的数据。列表框具有绑定到SQL Server的数据,其中所选索引是表“tblNamesOfCats”。它下面的按钮显示“选择类别”。用户应选择一个类别,然后单击按钮以查看该类别的表单。但是,我尝试了以下代码 -
Private Sub btnSelectCat_Click(sender As Object, e As EventArgs) Handles btnSelectCat.Click
If lbxCatList.SelectedItem = ("Action Figures") Then
frmCatsActionFigures.Show()
End If
End Sub
注意-btnSelectCat是按钮,lbxCatList是列表框,frmCatsActionFigures是Action Figures类别的表单。
然而,当我尝试调试这个时,我最终得到了这个错误 -
Overload resolution failed because no Public '=' can be called with these arguments:
'Public Shared Operator =(a As String, b As String) As Boolean':
Argument matching parameter 'a' cannot convert from 'DataRowView' to 'String'.
我应该使用什么代码?我该如何解决这个问题?
UPDATE(1) - 如前所述,Listbox由一个SQL Server表填充,该表只有一列,称为“类别名称”,行中只有每个类别的名称。 以下是错误的一些屏幕截图(显然你必须遵循链接,因为我没有足够的声誉来发布图片) -
https://sites.google.com/a/devincave.com/temporaryimagesite/
答案 0 :(得分:0)
Private Sub btnSelectCat_Click(sender As Object, e As EventArgs) Handles btnSelectCat.Click
If lbxCatList.SelectedItem.ToString() = "Action Figures" Then
frmCatsActionFigures.Show()
End If
End Sub
答案 1 :(得分:0)
将数据库表绑定到列表框时,datarowview(datasource)中的行存储在列表框中,而不是存储在这些行的列中的数据中。所以这不是文字。但DRV对象。在错误消息中:
Argument matching parameter 'a' cannot convert from 'DataRowView' to 'String'
“Action Figures”是“String”部分,SelectedItem是DRV对象行,所以你必须这样对待它。
' works for ONE col views or when the column you want is #0
' else use the right index
If lbxCatList.SelectedItem.Item(0).ToString = "Action Figures" Then
或
If lbxCatList.SelectedItem.Item("Category").ToString = "Action Figures" Then
或将其强制转换为drv行(上面基本上是简写):
Dim drv As DataRowView = lbxCatList.SelectedItem
If drv.Item("Category").ToString ' or use index of 0
使用后一版本,您可以在此处设置中断并将鼠标悬停在drv
上,以查看所有属性和值,以便找出您想要的位置以及如何获取它