Search code examples
c#sqlpetapoco

PetaPoco sample application for Windows forms


I came accross PetaPoco and decided to give it a try Did anyone come accross a sample WindowsForms app with this? So far I only found sample web application.

I understand the concept of PetaPoco that is similar to NHibranate but I failed to find an example to see how to bind a Grid to the result of the query. Best would be a sample app if anyone got one on hand.


Solution

  • You can bind a Winform DataGridView like this:

        Dim ppdb = New PetaPoco.Database("Data Source=XXXX;Initial Catalog=xxxx;Integrated Security=True;", "System.Data.SqlClient")
        Dim blTable = New BindingList(Of TableItem)(ppdb.Query(Of TableItem)("select * from table").ToList)
        Dim bsTable As New BindingSource(blTable, "")
        DataGridView.DataSource = bsTable
    

    You'll need to save the objects back to the database if you want to keep your changes.

    To get the POCO bound to a row, use:

    Dim obj = DataGridView.Rows(RowIndex).DataBoundItem