DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Fri, 10 Aug 2007 13:08:34 -0000,    posted on: microsoft.public.dotnet.framework.adonet        back       

Thread Index
  1    VueMme


DataGridView, BindingSource and Custom object   
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!
Date:Fri, 10 Aug 2007 13:08:34 -0000   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us