Home
Options

Is it possible to set background color of a row when you dump results to a grid?

I was hoping something like this would work

here's code:

void Main()
{
    var list =  Enumerable.Range(1, 10).Select(x => new { x, Formatted_x = Util.RawHtml($"<div style='background-color: lightgreen'>{x}</div>")});

    list.Dump();
    list.Dump(true);
}

Comments

  • Options

    improved style so the actual table cell is colored instead of the div within

    void Main()
    {
        Console.Write(Util.RawHtml(@"<style>
        td:has(> span.red) {background-color:pink}
        td:has(> span.green) {background-color:lightgreen}
        </style>"));
    
        var list = Enumerable.Range(1, 10).Select(x => new { x, Formatted_x = Util.RawHtml($"<span class='{(x>5? "green" :"red")}'>{x}</span>")});
    
        list.Dump();
        list.Dump(true);
    }
    
    
  • Options

    You can do this more easily with Util.Highlight:

    from i in Enumerable.Range (0, 10)
    select new { i, formatted = Util.Highlight (i, i > 5 ? "pink" : "lightgreen") }
    

    Or to highlight the entire row:

    from i in Enumerable.Range (0, 10)
    select Util.Highlight (new { i, formatted = i }, i > 5 ? "pink" : "lightgreen")
    

    This will also have the benefit of being ignored when dumping to data grids, so you will see the data rather than the HTML. To answer why the grid doesn't display cells in the highlight color, there's probably no reason why it couldn't. I'll look into whether this feature can easily be added. Note, however, that it would work only with Util.Highlight and not CSS styles in general because the grid renders via a rich client control, not a web browser.

  • Options
    edited June 2023

    thanks man!

    maybe if there's room in your backlog, once you get the datagrid to recognize highlight color... this can be exported to excel too? just a thought.

Sign In or Register to comment.