DataGridView, BindingSource and Custom object

DataGridView, BindingSource and Custom object

Post by VueMm » Sat, 11 Aug 2007 22:08:34


i 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(custo
 
 
 

1. Adding new row to DataGridView bound to Custom Object

2. Supress BindingSource/DataGridView SelectionChanged on new DataSou

(Framework 2.0, VS2005, XP sp2 all fully patched)
I have a Form with a DataGridView and a BindingSource, among other controls.
The DataSource of the DataGridView is the BindingSource. The DataSource of
the BindingSource is a List(Of myclass). So far, so good.

In response to user action on the other controls, the List(Of myclass) may
be re-populated. When this happens, I execute "BindingSource1.ResetBindings".
I do this as well when the List(Of myclass) is resorted based on a
ColumnClick on the DataGridView.
Again, so far, so good.

The Problem is that Resetbindings causes multiple SelectionChanged events
and multiple CurrentChanged events to be raised, though not necessarily an
equal number of each. I only wish to handle USER caused SelectionChanges (or
CurrentChanged) events.

In other words, I wish to supress or ignore such events that occur as a
side-effect of reloading/sorting the List(Of myclass). At the very worst, I
would like to set some property or change my code such that I only get ONE of
each event per reload of the List(Of myclass). I know how to deal with it if
only one such event is raised. Irregular multiple occurances are much more
difficult to deal with.

In the DataGridView FAQ, there are words to the effect that Event Raising
can be suppressed. I have no idea where to even look for how to do this.

Any help will be appreciated.
--
Jim Parsells

3. Changes in BindingSource not reflected in DataGridView

4. vb.net changing bindingsource datasource and datagridview datasource at runtime

5. Supress BindingSource/DataGridView SelectionChanged on new Dat

6. DataGridView - BindingSource: How can i do a refresh?

7. DataGridView bound to BindingSource will not display data

8. FIXED DataGridView bound to BindingSource will not display data

9. bindingsource datagridview dataerror

10. DataTable, BindingSource and DataGridView

11. Bind DataGridView with BindingSource with .net 2.0

12. DataGridView/BindingSource - new row position (IsCurrentRowDirty is false) causes data binding issue

13. Binding multiple DataGridViews to one BindingSource

14. Linq, Datagridviews and Bindingsources

15. Problem with DataGridView and BindingSource