Cell color in Eto with Python

rhinopython
eto
python

#1

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()

Eto Forms: Sorting GridView from column heading click?
(Dale Fugier) #2

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

Terrific, thanks!