How to handle GridView.SelectedRowsChanged in EtoForms?


I’m using a GridView() in my EtoForm(Modeless) to display a simple list of entries. My aim is to update the Label at the bottom to function like a status bar, and show data from the selected row. But, my Rhino crashes if I try to select the rows.

Can someone let me know what needs to change, to get this working?

GH_File: (3.9 KB)

import Rhino, System
from Grasshopper.Instances import EtoDocumentEditor
from Eto import Forms, Drawing

class SampleForm(Forms.Form):

    def __init__(self):
        self.GridView = self.load_GridView()
        # Rhino is not happy with this line.
        #self.GridView.SelectedRowsChanged += self.OnSelected

        self.Label = Forms.Label(Text='Nothing is Selected...', BackgroundColor=Drawing.Color.FromArgb(100, 150, 0, 75))

        layout = Forms.DynamicLayout()
        self.Content = layout

    def load_form(self):

        self.Title = 'Award Nomination'
        self.ClientSize = Drawing.Size(300, 300)
        self.Padding = Drawing.Padding(15)
        self.Owner = EtoDocumentEditor
        self.Topmost = True
    def load_GridView(self, multi_select=True, col_reorder=True):

        grid_view = Forms.GridView()
        grid_view.Size = Drawing.Size(250,250)
        grid_view.AllowMultipleSelection = multi_select
        grid_view.AllowColumnReordering = col_reorder

        colA = Forms.GridColumn()
        colA.HeaderText = '+'
        colA.Editable = True
        colA.DataCell = Forms.CheckBoxCell(0)

        colB = Forms.GridColumn()
        colB.HeaderText = 'Name'
        colB.Editable = False
        colB.DataCell = Forms.TextBoxCell(1)

        colC = Forms.GridColumn()
        colC.HeaderText = 'Age'
        colC.Editable = False
        colC.DataCell = Forms.TextBoxCell(2)

        colD = Forms.GridColumn()
        colD.HeaderText = 'Occupation'
        colD.Editable = False
        colD.DataCell = Forms.TextBoxCell(3)

        self.add_data_to_grid(grid_view )

        return grid_view

    def add_data_to_grid(self, parent):
        DataStore = []
        DataStore.append([True, 'Matt', 27, 'Scientist'])
        DataStore.append([True, 'John', 22, 'Engineer'])
        DataStore.append([True, 'Mike', 49, 'Professor'])
        parent.DataStore = DataStore

    def OnSelected(self, sender, args):
        self.Label.Text = sender.SelectedItem.Values[1]

if x:
    dlg = SampleForm()


~ ~ ~ ~ ~ ~ ~
Kaushik LS
Chennai, IN

Hi @Kaushik,

the crash happens because of the code in the event, not by adding the event. Your DataStore holds a list of lists, if you try to access sender.SelectedItem it does not have a property Values. You can access by index directly. To prevent the crash, put the event code into try/except blocks and print the exception eg:

def OnSelected(self, sender, args):
        if isinstance(sender.SelectedItem, list):
            self.Label.Text = sender.SelectedItem[1]
    except Exception as ex:
       print ex


1 Like

Wow, I can’t believe I overlooked this. I was trying the same with a TreeGridView() earlier, and I thought it had the same structure. Thanks for pointing it out.