【问题标题】:conversion from string to type boolean is not valid从字符串到布尔类型的转换无效
【发布时间】:2011-10-24 10:09:17
【问题描述】:

错误发生在If wba.Selected = "MARKETING CODE" Then 线上...希望有人能提供帮助。谢谢

'Private Sub NewRecord_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    markCodes = New DataGridViewComboBoxColumn()
    markCodes.Name = "Marketing Codes"
    Me.DataGridView1.Columns.Add(markCodes)
    markCodes.Visible = False
End Sub

'Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)

    If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns(wba.Name).Index Then
        If TypeOf e.Control Is ComboBox Then
            cbxWba = TryCast(e.Control, ComboBox)
            RemoveHandler cbxWba.SelectionChangeCommitted, New EventHandler(AddressOf cbxWba_SelectionChangeCommitted)
            AddHandler cbxWba.SelectionChangeCommitted, New EventHandler(AddressOf cbxWba_SelectionChangeCommitted) 'this event will fire up if there's a selected item.
        End If
    End If

End Sub

'Private Sub cbxWba_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs)

    loadDescriptions()

End Sub

Private Sub loadDescriptions()

    If wba.Selected = "MARKETING CODE" Then

        markCodes.Visible = True
        Try
            Dim con As New SqlConnection
            con.ConnectionString = ""
            Dim myCommand1 As New SqlClient.SqlCommand
            Dim myAdapter1 As New SqlClient.SqlDataAdapter
            Dim sql As String = "Select analysis_a + ' - ' + [desc] as Expr1 from marketingCode"
            Dim ds As New DataSet
            myCommand1.Connection = con
            myCommand1.CommandText = sql
            myAdapter1.SelectCommand = myCommand1
            myCommand1.Connection.Open()
            myAdapter1.Fill(ds)
            myCommand1.Connection.Close()
            Dim dt As New DataTable
            dt = ds.Tables(0)


            For Each row As DataRow In ds.Tables(0).Rows
                DataGridView1.CurrentRow.Cells(description.Name).Value = row("Expr1").ToString()
                markCodes.DataSource = dt
                markCodes.DisplayMember = "Expr1"
                markCodes.ValueMember = "Expr1"
            Next


        Catch
            MsgBox("failed")
        End Try

    End If



End Sub

【问题讨论】:

  • 如果您发布代码、XML 或数据示例,在文本编辑器中突出显示这些行,然后单击编辑器上的“代码示例”按钮 ({ })工具栏以很好地格式化和语法突出显示它!

标签: vb.net


【解决方案1】:

什么是wba? .Selected 听起来像是在将布尔属性与字符串进行比较。

查看您的代码,我假设 wba 是一个 datagridviewrow,然后您不能以这种方式使用 .Selected。 要检查 datagridview 中第一个选定单元格的值:

        With DataGridView1
            If .SelectedCells.Count > 0 Then
                If .SelectedCells(0).Value = "MARKETING CODE" Then
                    'we have a hit
                End If
            End If
        End With

或者,如果您已经知道它是什么单元格并且已将所选单元格存储在 wba 变量中:

                If wba.Value = "MARKETING CODE" Then
                    'we have a hit
                End If

如果您有 selectionmode=fullrowselect 并想检查第一个选定的 datagridviewrow 中的第一个单元格是否有值:

        With DataGridView1
            If .SelectedRows.Count > 0 Then
                If .SelectedRows(0).Cells(0).Value = "MARKETING CODE" Then
                    'we have a hit
                End If
            End If
        End With

如果您将行存储在 wba 中,并想检查该行中的第一个单元格是否具有特定值,则如下所示:

        If wba.Cells(0).Value = "MARKETING CODE" Then
            'we have a hit
        End If

【讨论】:

  • 感谢您强调这一点。我将代码更改为if cbxWba.SelectedValue = "MARKETING CODE" Then 结果它工作。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-19
  • 1970-01-01
  • 1970-01-01
  • 2021-05-05
  • 2013-10-31
  • 2011-07-30
  • 2015-02-09
相关资源
最近更新 更多