DataGridView, BindingSource and Custom object

V

VueMme

Hi all,
I have a question on how to write directly to the database the new,
the edit or the deleted records. I try to explain:
I have created a business object that hinerit form
INotifyPropertyChanged, and bind it to a DataGridView by a
BindingSource. I want that when the user delete a row it will be
directly deleted from the database, and the same if the user edit a
row or add a new record in the DataGridView.

Here's my code:
// ###########################

// My custom object

using System.Collections.Generic;
using System.ComponentModel;

public class Customer : INotifyPropertyChanged
{
private Guid _id=Guid.Empty;
private string _name = string.Empty;

public Guid id
{
get { return _id; }
set { _id = value; }
}

public string name
{
get { return _name; }
set
{
if (!value.Equals(_name))
{
_name = value;
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs("name"));
}
}
}

public static List<Customer> GetEntityList()
{
// get the customer list from the db
// ...
}

public static Customer GetEntity(Guid id)
{
// get the a customer from the db
// ...
}

public static Customer SaveEntity(Customer customer)
{
if (customer.id!=Guid.Empty)
return UpdateEntity(customer);
else
return AddEntity(customer);
}

public static Customer AddEntity(Customer customer)
{
// add the customer in the db
// ...
}

public static bool AddEntityList(List<Customer> customerList)
{
// add a list of customer in the db
// ...
}

public static Customer UpdateEntity(Customer customer)
{
// edit a customer in the db
// ...
}

public static void DeleteEntity(Customer customer)
{
// delete a customer from the db
// ...
}

}


// My form
public class frmCustomers : Form
{
public frmCustomers()
{
InitializeComponent();
SetupBindings();
}

private void SetupBindings()
{
BindingList<Customer> customerList =
new BindingList<Customer>(Customer.GetEntityList());
customersBindingSource.DataSource = customerList;
}

private void Save()
{
customersBindingSource.EndEdit();

Customer customer = customersBindingSource.Current as
Customer;
Customer.SaveEntity(customer);

BindingList<Customer> customerList =
new BindingList<Customer>(Customer.GetEntityList());
customersBindingSource.DataSource = customerList;
}

private void Delete()
{
Customer customer = customersBindingSource.Current as
Customer;

if (customer == null)
return;

DialogResult dlg = MessageBox.Show(
string.Format("Delete customer" + Environment.NewLine
+
"\"{0}\"?", customer.Name),
Constants.AppName,
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2);
if (dlg == System.Windows.Forms.DialogResult.Yes)
{
Customer.DeleteEntity(customer );
customersBindingSource.RemoveCurrent();
}
}
}
// ########## END OF CODE #############

Now I have to call Save() or Delete(), what I want is to save or
delete record automatically the record when the user change row or
delete it (Like the way used in the Microsoft Access forms).

Thanks and sorry for my english...

VueMme!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top