Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a databound grideview where I want to color-code the cells in my aspx.vb back end, based on criteria... I started with:

VB
If IsDBNull(DataBinder.Eval(e.Row.DataItem, "SwitchPort1")) = True Then
            e.Row.Cells(3).ForeColor = Drawing.Color.Black

Else
            e.Row.Cells(3).BackColor = Drawing.Color.Green

End If


***

This worked, so then I decided to add some ElseIf statements to it, and have it look at the difference between NullOrEmpty, but cannot get the syntax right. There are several examples, but for whatever reason, I am not getting past the first "If", nothing else is being evaluated. I test the database values to force a change in the cell color, and cannot make the cells do anything but White with Black Text (if db is null) OR Green Background (Else). Can anyone help me figure out what I am doing wrong? "Guest" is a specific value, for which I want to search, and if present - a specific color.

VB
If (e.Row.RowType <> DataControlRowType.DataRow) Then
    Exit Sub
End If
If IsDBNull(DataBinder.Eval(e.Row.DataItem, "SwitchPort1")) = True Then
    e.Row.Cells(3).ForeColor = Drawing.Color.Black
ElseIf Len(DataBinder.Eval(e.Row.DataItem, "SwitchPort1")) = 0 Then
    e.Row.Cells(3).BackColor = Drawing.Color.Black
ElseIf DataBinder.Eval(e.Row.DataItem, "SwitchPort1").ToString = "" Then
    e.Row.Cells(3).BackColor = Drawing.Color.Blue
ElseIf DataBinder.Eval(e.Row.DataItem, "SwitchPort1").ToString = "Guest" Then
    e.Row.Cells(3).BackColor = Drawing.Color.Yellow
Else
    e.Row.Cells(3).BackColor = Drawing.Color.Green
End If


What I have tried:

I have tried looking at various different sites to find references, but nothing is really explaining things in a way I understand. I have tried testing numerous lines of changes, Dim String statements, and testing the String, but I am just hitting a brick wall.
Posted
Updated 2-Jul-18 22:30pm
v2
Comments
Patrice T 2-Jul-18 19:37pm    
What do you mean by: 'but cannot get the syntax right.'
Describe problem, error message, position

1 solution

If i understand you well... i'd suggest to simplify this by using Dictionary class[^].

See:
VB.NET
'define "color translator" ;)
Dim ColorTranslator As Dictionary(Of String, Drawing.Color) = New Dictionary(Of String, Drawing.Color)
'add keys and values
With ColorTranslator
	.Add("-1", Drawing.Color.Black)
	.Add("0", Drawing.Color.Black)
	.Add("", Drawing.Color.Blue)
	.Add("Guest", Drawing.Color.Yellow)
End With

'...
'later
'...

Dim sVal As String = DataBinder.Eval(e.Row.DataItem, "SwitchPort1").ToString()
e.Row.Cells(3).BackColor = If(Not ColorTranslator.ContainsKey(sVal), Drawing.Color.Green, ColorTranslator(sVal))
 
Share this answer
 
v2
Comments
Member 13895589 3-Jul-18 9:53am    
THANK YOU!!!! That worked perfectly!
Maciej Los 3-Jul-18 10:43am    
I'm glad i could help.
Member 13895589 3-Jul-18 11:13am    
If I may ask, is there a way to call a 'portion' or the Dictionary value? I used your answer to work through what I am seeing, and if the cell in SQL is nchar(10), then in order to get the "Guest" to work, I have to use "Guest " (so there are 10 total characters, including spaces), in order for the Eval to correctly identify the Dictionary value. Once you explained this method, I was able to expand on the Dictionary, and have added several things, which is awesome. My fear is, someone will enter "Camera" instead of "Cameras", so the Dictionary would need an item of both
.Add("Camera ",Drawing.Color.Blue) and
.Add("Cameras ",Drawing.Color.Blue) in order to achieve the same affect. I tried to use the Eval to say "If(Not ColorTranslator.ContainsValue(sVal), …" and played with various ways to say "like or contains", used wildcards, but cannot figure out the syntax. Are you aware of a way to achieve this goal?
Maciej Los 3-Jul-18 11:29am    
There's no way to workaround it using widlcards. You have to add all keys and coresponding values. On the other hand, a word "Camera" is a part of "Cameras". So, you can pass 6-length substring to the dictionary. ;)
Member 13895589 3-Jul-18 11:51am    
"pass 6-length substring to the dictionary"... the statement
Dim sVal As String = DataBinder.Eval(e.Row.DataItem, "SwitchPort1").ToString()
e.Row.Cells(3).BackColor = If(Not ColorTranslator.ContainsKey(sVal), Drawing.Color.Green, ColorTranslator(sVal))


is currently pulling only exactly what is in the field... so if there are 5 spaces trailing the visible characters, the eval statement isn't finding the value in the dictionary. So, if I don't get "cameras" with 3 spaces trailing, the lookup fails. How do I pass a specific character length?

I understand the concept of LEN(… but not when also trying to eval the data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900