Cell color in Eto with Python

Can I get some hints how to make a cell background color different in an Eto GridView object? As well as the text color in a particular cell?

import Eto.Forms as forms
import Eto.Drawing as drawing
import Rhino.UI


class TestDialog(forms.Dialog):
    def __init__(self):
        self.Title = "Cell Colors"
        self.Size = drawing.Size(450,565)
        
        self.grid = forms.GridView()
        self.grid.Size = drawing.Size(300,425)
        
        #COLUMNS
        numberColumn = forms.GridColumn()
        numberColumn.HeaderText = "Row\t"
        numberColumn.DataCell = forms.TextBoxCell(0)
        
        nameColumn = forms.GridColumn()
        nameColumn.HeaderText = "Name\t"
        nameColumn.DataCell = forms.TextBoxCell(1)
        
        colorColumn = forms.GridColumn()
        colorColumn.HeaderText = "Color\t\t"
        colorColumn.DataCell = forms.TextBoxCell(2)
        
        self.grid.Columns.Add(numberColumn)
        self.grid.Columns.Add(nameColumn)
        self.grid.Columns.Add(colorColumn)
        
        self.grid.DataStore = [['0', 'Test', 'Make me red'], ['1', 'Test', 'Green'], ['2', 'Test', 'Blue']]
        
        layout = forms.DynamicLayout()
        layout.AddRow(self.grid)
        self.Content = layout


def main():
    dialog = TestDialog()
    dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)

if __name__ == "__main__":
    main()
1 Like

Hi @Tim_Williams,

After creating your GridView control, add a CellFormatting event like this:

self.grid.CellFormatting += self.OnCellFormatting

Then implement your formatting here:

def OnCellFormatting(self, sender, e): 
    e.ForegroundColor = drawing.Colors.White 
    if e.Row == 0:
        e.BackgroundColor = drawing.Colors.Red 
    elif e.Row == 1:
        e.BackgroundColor = drawing.Colors.Green 
    elif e.Row == 2:
        e.BackgroundColor = drawing.Colors.Blue 

I’ve modified your code (attached).

test_tim_williams.py (1.7 KB)

– Dale

3 Likes

Terrific, thanks!

Hey @dale, How can we go over the Rows in a for loop? there is only e.Row. there is no e.Rows. and at the same time, we cant set the e.Row = i.
I need to color rows that have 0 values in them.
thanks,
Hanan

Hi @tanasra,

The Grid.CellFormatting event occurs when each cell is being formatted for font and color. The event handler is passed a GridCellFormatEventArgs object, which incluides the actual cell item. From this you should be able to determine what you need.

– Dale

hey @dale, I wonder if we can color specific rows like that
rowsToColor = [3,4,7]

            foreach (int row in rowsToColor)
            {
                if (e.Row == row )
                    e.BackgroundColor = Colors.LightGrey;
            }

it is not working for me, but I need these specific rows to be colored. (rowstoColors is a list that changes each time…)

thanks,
Hanan

How about this?

private readonly int[] m_colorful_rows = new[] { 3, 4, 7 };

private bool IsColorfulRow(int row)
{
  // Linq...
  return m_colorful_rows.Contains(row);
}

private void OnGridCellFormatting(object sender, GridCellFormatEventArgs e)
{
  if (IsColorfulRow(e.Row))
    e.BackgroundColor = Colors.LightGrey;
}

– Dale