Eto Forms: Sorting GridView from column heading click?

eto

(Chris Hanley) #1

I can’t seem to find any documentation on how to sort a gridview on an eto form, by clicking on the column header? Is this possible? I can reorder the columns, but I can’t get them to sort. Here is the example I am working with, (taken from Cell color in Eto with Python)


This code is being run from a ghpython component. It’s just for testing at the moment to learn a little more about eto forms. Any suggestions on sorting the contents would be appreciated!
Eto_Grid.gh (3.4 KB)


(Giulio Piacentino) #2

I really don’t know, but probably @curtisw will be able to help.


(Dale Fugier) #4

Hi @chanley,

The GridView control has a ColumnHeaderClick event that you will want to handle. In the handler, you’d figure out what column was clicked and then sort your grid’s data store accordingly.

Does this help?

– Dale


(Chris Hanley) #5

@dale, yes, that helps! I was able to get it started. First, (as you suggested) was to add the event handler

self.grid.ColumnHeaderClick += self.OnColumnHeaderClick

Then used python’s sorted function , (with itemgetter), to handle the data sorting:

GridData2 = sorted(GridData, key=itemgetter(sortItem))

Lastly, the code for the event handler, (which seems a bit longwinded).

def OnColumnHeaderClick(self, sender, e):
    GridData = [list(i) for i in zip(Names, Ids, Memory)]
    if e.Column.HeaderText == "Service\t":
        sortItem = 0
        GridData2 = sorted(GridData, key=itemgetter(sortItem))
        self.grid.DataStore = GridData2
    elif e.Column.HeaderText == "PID\t":
        sortItem = 1
        GridData2 = sorted(GridData, key=itemgetter(sortItem))
        self.grid.DataStore = GridData2
    elif e.Column.HeaderText == "Memory\t\t":
        sortItem = 2
        GridData2 = sorted(GridData, key=itemgetter(sortItem))
        self.grid.DataStore = GridData2
    else:
        pass

I had originally thought that there was a sortorder property, but I think that was for windows forms?

The last thing I need to do is to make it sort ascending and descending based on click, (first click == ascending, second click == descending). Thanks again for pointing me in the right direction, and any suggestions are always welcome.

sample file attached.Eto_Grid.gh (7.3 KB)


#6

To simplify the OnHeaderClick, you can do this:

sortItem = self.grid.Columns.IndexOf(e.Column)

instead of all the if statements.


(Chris Hanley) #7

Thanks @Tim_Williams ! Much cleaner.