0

How to commit changes done in DataGridView using PetaPoco ? Something like :

namespace PetaPocoTest
{
    public partial class Form1 : Form
    {
        PetaPoco.Database db = new PetaPoco.Database("PgConnection");

        IEnumerable<customers> allCustomers;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            allCustomers = db.Query<customers>("SELECT * FROM customers");
            mGrid.DataSource = allCustomers .ToList();            
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            db.Save("customers", "custumer_id", allCustomers);
        }
    }
}
EmirZ
  • 646
  • 4
  • 10
  • 21
  • db.Save does not work. I get "The object doesn't have a property matching the primary key column name 'customer_id'" – EmirZ Dec 19 '13 at 18:54
  • I did try moving .ToList() above, and I still get "The object doesn't have a property matching the primary key column name 'customer_id'" – EmirZ Dec 19 '13 at 18:57

2 Answers2

1

Try my answer Here

This is the simplest and easiest and can do what you want so far

Community
  • 1
  • 1
Polamin Singhasuwich
  • 1,646
  • 2
  • 11
  • 20
0

I finally got it. I just wasted hours on something so obvious :\

namespace PetaPocoTest
{
    public partial class Form1 : Form
    {
        PetaPoco.Database db = new PetaPoco.Database("PgConnection");

        IEnumerable<customers> allCustomers;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            allCustomers = db.Query<customers>("SELECT * FROM customers");
            mGrid.DataSource = allCustomers .ToList();            
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
             foreach (var a in allCustomers)
             {
               db.Save("customers", "custumer_id", a);
             }
        }
    }
}
EmirZ
  • 646
  • 4
  • 10
  • 21
  • Nop... this is not good! It works but it generates and executes UPDATE-s for all rows no matter if they are modified or not ! – EmirZ Dec 20 '13 at 19:09
  • Micro ORMs generally don't track changes. You'll need to implement this yourself. I'd suggest you look for a modification event and flag each customer that gets changed as updated. Then upon save, only persist the customers which have changed – Plebsori Jan 11 '16 at 09:18