Changing row color based on a string value

Using the following code:

args.Attributes.Add("style", $"background-color: {(args.Data.Deals_SaleTp = "New" ? "#90EE90" : "white")};");

Deals_SaltTp is a nchar() in sql.

Keeps generating the following error.

dotnet: c:\smg\server\Pages\DealLog.razor.designer.cs(315,89): error CS0029: Cannot implicitly convert type 'string' to 'bool' [c:\smg\server\Smg.csproj]

I have checked several threads, but can't find out how to compare a character field to a string value.

Side Note: Looking everywhere for an answer. If I use == the error goes away, but the Style does change. If I use = I get the "error CS0029: Cannot implicitly convert type 'string' to 'bool'" message. None of the fields are bool and I need to compare them to a string value in the Deals_SaleTp field.

Hi,
Can you try

if (args.Data.Deals_SaleTp == "New" )
    args.Attributes.Add("style", $"background-color: {"#90EE90"};");
else
     args.Attributes.Add("style", $"background-color: {"white"};");

There are quite a few issues here:

  1. First thing is that == is always required for comparison in C# (= is used for assignments). This is just C# basics.
  2. The actual value of Deals_SaleTp is probably not "New" as nchar columns have padding characters. So unless the column is nchar(3) the actual value of the property will end with some whitespace characters. This can be easily checked via debugging. If this is the case you should use StartsWith: args.Data.Deals_SaleTp.StartsWith("New")
  3. You can't set the background color of a row in the RowRender event.. You didn't say what event you are using but if it is RowRender you should use CellRender instead.