[IronPython] Data binding - how?

Dave Fugate dfugate at microsoft.com
Thu Dec 7 03:18:00 CET 2006


I looked into trying to replace the Python class full of properties with a list full of tuples.  That is, doing something similar to:
        #...
        data = [
            ('Joe', 23),
            ('Bob', 8),
            ('Thomas', 32),
            ('Patrick', 41),
            ('Kathy', 19),
            ('Sue', 77),
            ]
        #...
        grid.DataSource = data

as was suggested in an earlier email.  As far as I can tell you're limited to one-dimensional arrays for the DataSource as is suggested by http://www.vbdotnetforums.com/showthread.php?t=14657.


From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Wednesday, December 06, 2006 5:43 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Data binding - how?

As far as I can tell the issue here seems to be that the data source is getting the column names from the list and not from the individual elements that are being added to the list.  For example I tried the code below which hooks the ColumnAdded event to see what columns are actually getting added.  In that case we end up adding columns for all of the methods on the List class.  I seem to get the same behavior if I use an array for my data source as well.

We do implement ICustomTypeDescriptor on all user-defined types (both new-style and old-style) and this functionality does appear to work, eg:

x = MD('Foo', 23)
from System.ComponentModel import ICustomTypeDescriptor
for a in ICustomTypeDescriptor.GetProperties(x): print a.Name, a.GetValue(x)

prints:

name Foo
age 23
__doc__ None
__module__ __main__
__init__ <bound method MD.__init__ of <__main__.MD instance at 0x00000000000000FB>>
Name Foo
Age 23

But the DataGridView doesn't seem to be looking at the individual items.  Feel free to open a bug on this and please include the repros you had before.


import clr
clr.AddReference('System.Windows.Forms')
import System.Windows.Forms as SWF
from System import Array
from System.Collections.Generic import List
from System.ComponentModel import IListSource



class MD:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    @property
    def Name(self): return self.name
    @property
    def Age(self): return self.age

instType = type(MD('a', 2))

class Form(SWF.Form):
    def __init__(self):
        grid = SWF.DataGridView()
        self.grid = grid
        grid.DataSource = List[object]( ( MD('abc',23), ) )
        grid.Dock = SWF.DockStyle.Fill
        self.Controls.Add(grid)
        grid.ColumnAdded += self.ColumnAdded
    def ColumnAdded(self, sender, *args):
        print self, sender, args
        for x in self.grid.Columns: print x





From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Patrick O'Brien
Sent: Wednesday, December 06, 2006 1:48 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Data binding - how?

On 12/3/06, Luis M. González <luismg at gmx.net<mailto:luismg at gmx.net>> wrote:
I guess the problem is in the properties you defined in "Person".
I tried this script, but importing "Person" from an assembly writen in C#, and it worked.

So I assume that python properties are not recognized as .Net properties, and they are needed to create the columns of the datagridview control.

I've pretty much given up on getting data binding to work correctly with instances of Python classes, at least for now.  But it would be nice to know if I'm right or wrong, if I should file any bug reports, if there are any plans to make this work any time soon, etc.  Would anyone from the IronPython team care to comment?

--
Patrick K. O'Brien
Orbtech       http://www.orbtech.com
Schevo        http://www.schevo.org
Louie         http://www.pylouie.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20061206/4b811e48/attachment.html>


More information about the Ironpython-users mailing list