c# - 如何根据条件为 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>
解决方案
您可以使用nameof
来获取颜色的字符串名称。
var colorName = nameof(Brushes.Green);
推荐阅读
- javascript - 请求始终被 CORS 策略 c# net core 阻止
- arrays - 修改从 Node.js 中的 API 检索的 JSON 数据
- apache-spark - Spark - 如何根据最小洗牌的列重新分区数据帧?
- r - 选择只有一个值与参考不同的数据框行
- php - Foreach 循环为目录中的每个文件循环不工作
- ios - 从资产中检索文件名到数组中
- react-native - 在 FlatList 或 List 中表示以下数据
- xamarin - 如何在 Xamarin 表单中创建自定义抽屉?
- javascript - addEventListener 排除项
- excel - 将单元格移动到满足条件的特定工作表