views:

40

answers:

1

I have a datagridview with a datasource of type binding list.

I understand that when the datagridview changed this will update the items in the binding list.

I also know that if the objects in the bindinglist implement Inotifypropertychanged then if the obects are changed outside the grid then the objects will notify the bindlist which will then notify the datagrid

My question is:

If the datagrid view changing an object, I want the bindinglist or changed object to fire an event that allows me to pass the object to a WCF service that will persist the object on the data access layer side e.g. Service.Save(ChangedObject)

How would I go about doing this?

+1  A: 

Well, when a class implements INotifyPropertyChanged, the BindingList<T> class raises its ListChanged event on each PropertyChanged event with a ListChangedEventArgs object whose ListChangedType property is equal to ItemChanged.

Armed with this knowledge, I feel you should be able to accomplish what you want. (Where ListChangedType == ListChangedType.ItemChanged, pass the object at index NewIndex of the BindingList<T> to your service.)


UPDATE: Below is a sample application I wrote to demonstrate that the ListChanged event is raised when an item is modified through the DataGridView:

The CustomObject class: a simple implementation of INotifyPropertyChanged

Imports System.ComponentModel

Public Class CustomObject
    Implements INotifyPropertyChanged

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
            OnPropertyChanged("Name")
        End Set
    End Property

    Protected Sub OnPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

End Class

The DataBindingTestForm class: a basic Form class with a DataGridView bound to a BindingList(Of CustomObject)

Imports System.ComponentModel

Public Class DataBindingTestForm

    Private WithEvents _customObjects As New BindingList(Of CustomObject)

    Private Sub DataBindingTestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim obj As New CustomObject
        obj.Name = "John"
        _customObjects.Add(obj)

        obj = New CustomObject
        obj.Name = "Bill"
        _customObjects.Add(obj)

        MainDataGridView.DataSource = _customObjects
    End Sub

    Private Sub _customObjects_ListChanged(ByVal sender As Object, ByVal e As ListChangedEventArgs) Handles _customObjects.ListChanged
        If Not e.ListChangedType = ListChangedType.ItemChanged Then Return

        Notify("ListChanged event fired. Index: {0}, Value: {1}", e.NewIndex, _customObjects(e.NewIndex).Name)
    End Sub

    Private Sub Notify(ByVal format As String, ByVal ParamArray args() As Object)
        MsgBox(String.Format(format, args))
    End Sub

End Class

In running the above demo application, when I change the value in a cell of the DataGridView (e.g., from "John" to "George"), a message box appears with the following text:

ListChanged event fired. Index: 0, Value: George

Try this out for yourself and let me know if you run into any further problems.

Dan Tao
Thanks for the response.Let me repeat what I think you said so that I can clarify.As it is right now the grid is bound to the list. If I alter a cell in the grid it changes the appropriate value for that object in the list. How the ListChanged event is not raised.In order to raise the list changed event when the propery is changed in the grid. I need to implement INotifyPropertyChanged on the the property in the object.So the flow of events will be grid changes object in list, object fires INotify, list fires listchanged upon registering Inotify?
bearrito
@bearrito: If I understand you correctly, it seems you're saying the `ListChanged` event is not raised when you modify a value in the `DataGridView`. But it *should* be. See my updated answer for some simple example code to demonstrate this. If you are not seeing this behavior, are you sure you've implemented `INotifyPropertyChanged` properly? Otherwise, if you could post the code you have, I'd be better equipped to help you out.
Dan Tao