0

我现在通过服务器名称列表完成了所有工作,但我想通过列出的 True 或 False 值检查 SQL Server 中名为 Compliance 的列来添加 IF 语句。如果为 False,则名称会将文本颜色更改为红色。如果为 True,则名称不会更改文本颜色。我不确定如何在 VB 代码中添加它。我很确定我需要将 IF 语句放入While dr.Read()。我对 VB.Net 很陌生,不确定哪些 VB 代码会改变文本颜色。

这是我的VB代码,

Sub loadData()
    'clear treeview control
    TreeViewGroups.Nodes.Clear()

    'fetch owner data and save to in memory table
    Dim sqlConn As New System.Data.SqlClient.SqlConnection((ConfigurationManager.ConnectionStrings("SOCT").ConnectionString))
    Dim strSqlSecondary As String = "SELECT [Name] FROM [dbo].[ServerOwners] where SecondaryOwner like @uid order by [name]"

    'Getting a list of True or False from Compliance column
    Dim strSqlCompliance As String = "SELECT [Compliance] FROM [dbo].[ServerOwners] where SecondaryOwner like @uid order by [name]"

    Dim cmdSecondary As New System.Data.SqlClient.SqlCommand(strSqlSecondary, sqlConn)

    Dim cmdCompliance As New System.Data.SqlClient.SqlCommand(strSqlCompliance, sqlConn)

    cmdSecondary.Parameters.AddWithValue("@uid", TNN.NEAt.GetUserID())

    cmdCompliance.Parameters.AddWithValue("@uid", TNN.NEAt.GetUserID())

    Dim dr As System.Data.SqlClient.SqlDataReader
    Try
        sqlConn.Open()
        Dim root As TreeNode
        Dim rootNode As TreeNode
        Dim firstNode As Integer = 0
        'Load Primary Owner Node
        'Create RootTreeNode

        dr = cmdSecondary.ExecuteReader()
        If dr.HasRows Then
            'Load Secondary Owner Node
            'Create RootTreeNode
            root = New TreeNode("Secondary Owner", "Secondary Owner")
            TreeViewGroups.Nodes.Add(root)
            root.SelectAction = TreeNodeSelectAction.None

            rootNode = TreeViewGroups.Nodes(firstNode)
            'populate the child nodes
            While dr.Read()
                Dim child As TreeNode = New TreeNode(dr("Name"), dr("Name"))
                rootNode.ChildNodes.Add(child)
                child.SelectAction = TreeNodeSelectAction.None
            End While
            dr.Close()
            cmdSecondary.Dispose()
        End If

        'check if treeview has nodes
        If TreeViewGroups.Nodes.Count = 0 Then
            noServers()
        End If
    Catch ex As Exception
        hide()
        PanelError.Visible = True
        LabelError.Text = ex.ToString()
    Finally
        sqlConn.Dispose()
    End Try
End Sub
4

2 回答 2

1

您需要检查是否可以将合规值转换为布尔值,然后使用红色样式将 DIV 包裹在文本周围,如下所示:

While dr.Read()
    Dim child As TreeNode = New TreeNode(dr("Name"), dr("Name"))

    ' Test whether compliance value can be converted to Boolean type or not
    Dim complianceFlag As Boolean 

    If Boolean.TryParse(dr("Compliance"), complianceFlag) Then
        ' Yes, compliance value is a Boolean, now set color based on value
        If Not complianceFlag Then
            child.Text = "<div style='color:Red; float: left;'>" + child.Text & "&lt;/div>"
        End If
    Else
        ' Unable to convert compliance value to Boolean
        ' Do something here if you want or just ignore it
    End If

    rootNode.ChildNodes.Add(child)
    child.SelectAction = TreeNodeSelectAction.None
End While

注意:TryParse如果预期的转换失败,不会抛出异常,因此需要输出变量complianceFlag.

于 2013-11-04T15:49:04.770 回答
0

我得到了代码工作

Sub loadData()
    'clear treeview control
    TreeViewGroups.Nodes.Clear()

    'fetch owner data and save to in memory table
    Dim sqlConn As New System.Data.SqlClient.SqlConnection((ConfigurationManager.ConnectionStrings("SOCT").ConnectionString))
    Dim strSqlSecondary As String = "SELECT [Name], [Compliance] FROM [dbo].[ServerOwners] where SecondaryOwner like @uid order by [name]"

    Dim cmdSecondary As New System.Data.SqlClient.SqlCommand(strSqlSecondary, sqlConn)


    cmdSecondary.Parameters.AddWithValue("@uid", TNN.NEAt.GetUserID())


    Dim dr As System.Data.SqlClient.SqlDataReader


    Try
        sqlConn.Open()
        Dim root As TreeNode
        Dim rootNode As TreeNode
        Dim firstNode As Integer = 0
        'Load Secondary Owner Node
        'Create RootTreeNode    
        dr = cmdSecondary.ExecuteReader()

        If dr.HasRows Then
            'Load Secondary Owner Node
            'Create RootTreeNode
            root = New TreeNode("Secondary Owner", "Secondary Owner")
            TreeViewGroups.Nodes.Add(root)
            root.SelectAction = TreeNodeSelectAction.None

            rootNode = TreeViewGroups.Nodes(firstNode)
            'populate the child nodes
            While dr.Read()
                Dim child As TreeNode = New TreeNode(dr("Name"), dr("Name"))
                Dim complianceFlag As Boolean

                If Boolean.TryParse(dr("Compliance"), complianceFlag) Then
                    ' Yes, compliance value is a Boolean, now set color based on value
                    If Not complianceFlag Then
                        child.Text = "<div style='color:Red'>" + child.Text & "</div>"
                    End If
                Else

                End If
                rootNode.ChildNodes.Add(child)
                child.SelectAction = TreeNodeSelectAction.None
            End While
            dr.Close()

            cmdSecondary.Dispose()

        End If


        'check if treeview has nodes
        If TreeViewGroups.Nodes.Count = 0 Then
            noServers()
        End If

    Catch ex As Exception
        hide()
        PanelError.Visible = True
        LabelError.Text = ex.ToString()
    Finally
        sqlConn.Dispose()
    End Try
End Sub
于 2013-11-04T18:42:43.393 回答