首页 > 解决方案 > 如何根据条件为 VB.net 中的 DataTable 行添加颜色

问题描述

我想用颜色而不是字符串填充 Datatable 行。颜色是字符串值的表示。正如您在下面的代码中看到的,我已经尝试过颜色和画笔。我怎样才能做到这一点?

例如,在前端,如果 cib = "EXIST/ACTIVE",我希望用户看到颜色为 GREEN

如果 cib = "EXIST/ACTIVE" 显示黄色。

这些值来自数据库

颜色代表

hdfAccountNumber.Value = GenericManager.decryptQueryString(Request.QueryString("AccountNumber"))
        Dim CustomerChannels = GetCustomerDetails(hdfAccountNumber.Value)
        Dim ussd As String = CustomerChannels.USSD + "/" + CustomerChannels.USSDStatus
        Dim cib As String = CustomerChannels.CIB + "/" + CustomerChannels.BBGStatus
        Dim rib As String = CustomerChannels.RIB + "/" + CustomerChannels.RIBStatus
        Dim mobile As String = CustomerChannels.MOBILEBANKING + "/" + CustomerChannels.NEWMOBILEStatus
        Dim debitCard As String = CustomerChannels.CardStatus

        Dim ussdColor As Color
        Dim cibColor As Color
        Dim ribColor As Color
        Dim mobileColor As Color
        Dim debitCardColor As Color



        Dim ChannelTable As New DataTable
        ChannelTable.Columns.Add("USSD")
        ChannelTable.Columns.Add("CIB")
        ChannelTable.Columns.Add("RIB")
        ChannelTable.Columns.Add("MOBILE")
        ChannelTable.Columns.Add("DEBIT CARD")

        For Each row As DataGridViewRow In DataGrid1.Rows
            cib = row.Cells(1).Value
            If cib = "EXIST/ACTIVE" Then
                cibColor = Color.Green
                row.DefaultCellStyle.ForeColor = cibColor
            ElseIf cib = "EXIST/INACTIVE" Then
                cibColor = Color.Yellow
                row.DefaultCellStyle.ForeColor = cibColor
            Else
                cibColor = Color.Red
                row.DefaultCellStyle.ForeColor = cibColor
            End If

            ussd = row.Cells(0).Value
            If ussd = "EXIST/ACTIVE" Then
                ussdColor = Color.Green
                row.DefaultCellStyle.ForeColor = ussdColor
            ElseIf ussd = "EXIST/INACTIVE" Then
                ussdColor = Color.Yellow
                row.DefaultCellStyle.ForeColor = ussdColor
            Else
                ussdColor = Color.Red
                row.DefaultCellStyle.ForeColor = ussdColor
            End If

        Next

        ChannelTable.Rows.Add(ussdColor, cibColor, rib, mobile, debitCard)
        DataGrid1.DataSource = ChannelTable
        DataGrid1.DataBind()
    End Sub

数据网格

   <asp:UpdatePanel ID="UpdatePanel4" runat="server" class=""  style="color:#ffffff; margin-top: 5px; font-size: 12px">
             <ContentTemplate>
       <p  style="font-weight: 700; font-weight: 700; margin-left: 10px; font-weight: normal; font-size: 14px;">Channels<span> <asp:Button ID="availableChannel" runat="server" Text="Get channels"  CssClass="tdcolor" /> </span></p>
          <asp:DataGrid ID="DataGrid1" runat="server" style="width: 100%; margin-top: -8px; font-weight: 600; border: 1px solid #5c2684;">  
          </asp:DataGrid>         
    </ContentTemplate>
</asp:UpdatePanel>    

标签: c#vb.netdatatablecolors

解决方案


您可以使用nameof来获取颜色的字符串名称。

var colorName = nameof(Brushes.Green);

推荐阅读