How can I implement Eto.Forms.Filtercollection in python

Hi,

I’d like to refresh the datastore of my Eto.Forms.TreeGridView. I’ve read this topic and the documentation and appearently I can pass a FilterCollection object to the TreeGridView.DataStore attribute which notifies the GridView to refresh when the DataStore is updated.

However, I cannot seem to instantiate a Eto.Forms.Filtercollection. Can anyone help me with this?

Thanks,
Tim

#! python3
import Eto
from System.Collections.Generic import IList

my_tuple = (['a',1], ['b',2])  
fc = Eto.Forms.FilterCollection(my_tuple) '>>> TypeError: cannot instantiate an open generic type'

1 Like

Hi @timcastelijn,

FilterCollection is a generic type, so you have to specify what type of item it contains. Doing this should get that working for you:

fc = Eto.Forms.FilterCollection[object](my_tuple)

Hope this helps!

Hi @timcastelijn,

FilterCollection is a generic class and needs a type.

Something like this:

#! python3
import Eto
import System

class MyClass(System.Object):
    def __init__(self, a, b):
        self.m_a = a
        self.m_b = b
    
    @property
    def A(self):
        return self.m_a
    @A.setter
    def A(self, value):
        self.m_a = value
        
    @property
    def B(self):
        return self.m_b
    @B.setter
    def B(self, value):
        self.m_b = value
        
def CompareItems(x, y):
    if x.A < y.A:
        return -1
    elif x.A > y.A:
        return 1
    return 0

def Test():
    coll = Eto.Forms.FilterCollection[MyClass]()
    coll.Sort = CompareItems
    coll.Add(MyClass('d', 4))
    coll.Add(MyClass('a', 1))
    coll.Add(MyClass('c', 3))
    coll.Add(MyClass('b', 2))

    for i in coll:
        print("{} = {}".format(i.A, i.B))
    
Test()

– Dale

1 Like

Hi @curtisw , @dale,

Thanks for the information. I see this approach works for the GridView, but not for the TreeGridView since the DataStore attribute doesn’t accept Eto.Forms.FilterCollection as a type.

However, I did find the ReloadData() method that solves my problem.

#! python3
import Eto
import Rhino

class MyDialog(Eto.Forms.Dialog[bool]):

    def __init__(self):
        super().__init__()

        self.datastore = None
        self.grid = None

    def on_add_click(self, sender, e):
        item2 = Eto.Forms.TreeGridItem()
        item2.Values = (['my_name2']) 

        self.datastore.Add(item2)
        self.grid.ReloadData()

    def create(self):
        # button add
        btn_add_item = Eto.Forms.Button()
        btn_add_item.Text = 'Add'
        btn_add_item.Click += self.on_add_click

        # treegridview
        item = Eto.Forms.TreeGridItem()
        item.Values = (['my_name'])

        self.datastore = Eto.Forms.TreeGridItemCollection()
        self.datastore.Add(item)

        col1 = Eto.Forms.GridColumn()
        col1.HeaderText = 'name'
        col1.DataCell = Eto.Forms.TextBoxCell(0)

        self.grid = Eto.Forms.TreeGridView()
        self.grid.Columns.Add(col1)

        self.grid.DataStore = self.datastore

        # create layout
        base_layout = Eto.Forms.DynamicLayout()
        base_layout.AddRow(self.grid)
        base_layout.AddRow(btn_add_item)
        self.Content = base_layout


def main():

    dialog = MyDialog()
    dialog.create()

    rc = dialog.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)
    if not rc:
        return

if __name__ == '__main__':
    main()