0

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.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
Jester
  • 3,069
  • 5
  • 30
  • 44

3 Answers3

2

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
1

You should be able to just bind directly to the List<T> returned from public IEnumerable<T> Query<T>(string sql, params object[] args) - example taken from line 752 in PetaPoco.cs @ source on GitHub

bluevector
  • 3,485
  • 1
  • 15
  • 18
0

Even it's quite old thread, maybe someone will need this. Bind directly to List and that's it.

    using (var db = new PetaPoco.Database(string_connection, string_provider))
    {
        try
        {
            var ret = db.Query<MyTable>("select * from my_table_name").ToList();

            if(ret != null)
            {
                dgv.DataSource = ret; 
            }
        }
        catch (Exception ex)
        {
            Trace.WriteLine("Error: " + ex.Message); 
        }
    } // using
user1797147
  • 915
  • 3
  • 14
  • 33