简体   繁体   中英

How to add color to DataTable row in VB.net base on condition

I want to fill the Datatable row with color instead of a string. The color is a representation of the string value. I have tried Color and Brushes as you can see in the code below. How can I make this happen?

For instance, On the frontend, I want users to see color GREEN if cib = "EXIST/ACTIVE"

if cib = "EXIST/ACTIVE" display Yellow.

These values are coming from the Database

颜色代表

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

datagrid

   <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>    

You can use nameof to get the string name of the color.

var colorName = nameof(Brushes.Green);

try below code. You need to use the attributes of row.DefaultCellStyle.* to set row color and other formatting

 For Each row As DataGridViewRow In DataGrid1.Rows
    cib = row.Cells(1).value 
    If cib = "EXIST/ACTIVE" Then
        cibColor = Brushes.Green
        row.DefaultCellStyle.ForeColor = cibColor
    ElseIf cib = "EXIST/INACTIVE" Then
        cibColor = Brushes.Yellow
        row.DefaultCellStyle.ForeColor = cibColor
    Else
        cibColor = Brushes.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM