根据在列表框中选择的内容(列表框所选索引来自 SQL Server)对按钮进行编码以打开窗体



我正在构建一个应用程序,其中充满了购物等商品,用户(登录并选择"浏览类别")然后可以看到一个列表框和按钮(旁边还有一个按钮返回)。列表框包含类别名称,对于每个类别,都有一个窗体来保存该类别下项目的数据。列表框具有到 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'.

我应该使用什么代码?我将如何解决这个问题?

更新(1)-如前所述,Listbox 由一个 SQL Server 表填充,该表只有一列,称为"类别名称",并且这些行中只包含每个类别的名称。以下是一些错误的屏幕截图(显然您必须点击链接,因为我的声誉不够高,无法发布图像)-

https://sites.google.com/a/devincave.com/temporaryimagesite/

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

将数据库表绑定到列表框时,数据行视图(数据源)中的行存储在列表框中,而不是这些行中的列中的数据。 所以它不是文本。但 DRV 对象。 在错误消息中:

Argument matching parameter 'a' cannot convert from 'DataRowView' to 'String'
"

动作图"是"字符串"部分,选定项是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上以查看所有属性和值,以弄清楚您想要的位置以及如何获取它

最新更新