PC Review


Reply
Thread Tools Rate Thread

DataGridView, BindingSource and Custom object

 
 
VueMme
Guest
Posts: n/a
 
      10th Aug 2007
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!

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGridView - custom collection object Rob Microsoft ASP .NET 0 9th Jan 2009 11:22 AM
Adding new row to DataGridView bound to Custom Object =?Utf-8?B?VGVycnk=?= Microsoft VB .NET 4 8th Nov 2007 12:04 AM
BindingSource/Custom Object need HasChanges method timnels@gmail.com Microsoft C# .NET 1 21st Jun 2007 07:15 PM
DataGridView - BindingSource (Object) and ComboBox cell Przemek M. Zawada Microsoft C# .NET 3 24th Feb 2007 01:05 PM
BindingSource, Custom DataSource and DataGridView driving me mad! Steve Microsoft C# .NET 2 21st Jun 2006 07:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:45 AM.