从字符串到布尔类型的转换无效 [英] conversion from string to type boolean is not valid

查看:36
本文介绍了从字符串到布尔类型的转换无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The error occurs on the line If wba.Selected = "MARKETING CODE" Then ... Hopefully someone might be able to help. Thanks

'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

推荐答案

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

What is wba? The .Selected sounds like you are comparing a boolean property to an string.

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

Looking at your code, I assume that wba is a datagridviewrow, and then you cant use .Selected in that way. To check for a value of the first selected cell in the 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 变量中:

or, if you allready know what cell it is and has stored the selected cell in the wba-variable:

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

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

If you have selectionmode=fullrowselect and want to check if first cell in the first selected datagridviewrow has a value:

        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 you have the row stored in the wba, and want to check if the first cell in that row has a specific value then its like this:

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

这篇关于从字符串到布尔类型的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆