views:

45

answers:

1

I am trying to figure out how to make use IronPython (2.6) wpf and sqlite3 (i use the Ironpython.SQLite.dll)

I get somewhat stuck in how to use the wpf datagrid.

Can anyone help me further in how to code in order to fill the datagrid

with the data from the sqlite database thats made here.

Below is the code on how far i got... 1st the python xaml file

                <Grid>
                      <DataGrid Name="mydatagrid" ItemsSource="{Binding}" ColumnWidth="300" RowHeight="20" AutoGenerateColumns="True" Margin="12,370,242,25"  GridLinesVisibility="All" CanUserResizeRows="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding}" Header="Header" />
                            <DataGridTextColumn Header="Header" />
                            <DataGridTextColumn Header="Header" />
                        </DataGrid.Columns>
                      </DataGrid>
                </Grid>
            </Window> 

Ant then below here the python code
myapp.py import sys import nt import clr

            clr.AddReference('PresentationFramework')

            from System.IO import FileMode, FileStream
            from System.Windows import Application
            from System.Windows.Markup import XamlReader

            stream = FileStream('Myxaml.xaml', FileMode.Open)
            window = XamlReader.Load(stream)
            window.Title='My Program'

            sys.path.append(nt.getcwd())
            clr.AddReferenceToFile("IronPython.SQLite.dll")
            sys.path.append("C:\Program Files (x86)\IronPython 2.6 for .NET 4.0\Lib") 

            import os
            import _sqlite3
            from _sqlite3 import *


            DB_NAME = 'mydb.s3db'

            if not os.path.exists(DB_NAME):
                #create new DB if not exist, 
                con = _sqlite3.connect(DB_NAME)
                con.execute(''' CREATE TABLE [mytable] 
                (
                    [key] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,
                    [desc] NVARCHAR(30)  NULL,
                    [count1] FLOAT DEFAULT '0' NULL,
                    [count2] FLOAT DEFAULT '0' NULL
                    )''')        
                con.execute('''insert into mytable (desc,count1,count2) values ("part1",1.4,10)''')
                con.execute('''insert into mytable (desc,count1,count2) values ("part2",2.4,20)''')
                con.execute('''insert into mytable (desc,count1,count2) values ("part3",3.4,30)''')
                con.execute('''insert into mytable (desc,count1,count2) values ("part4",4.4,40)''')
                con.execute('''insert into mytable (desc,count1,count2) values ("part5",5.4,50)''')
                con.execute('''insert into mytable (desc,count1,count2) values ("part6",6.4,60)''')
                con.commit()
            else:
                #use existing DB
                con = _sqlite3.connect(DB_NAME)

            grid1 = window.FindName('mydatagrid')
            cur=con.cursor()
            cur.execute("SELECT * FROM mytable")
            #grid1.ItemsSource=cur.fetchone()
            #grid1.DataContext=cur.fetchall()
            #print cur.fetchall()
            app = Application()
            app.Run(window)

I am a starter in this and might be completely on the wrong path, but i cant seem to find out what or how to fill the itemssource or datacontext or both... with anyway not with the commentet out options above......

+1  A: 

You must load your data into a class instance that is bound to the datagrid. Here is my article about WPF databinding in IronPython. Converting it to use DataGrid should be easy ;-)

Lukas Cenovsky