Click here to Skip to main content
15,887,434 members
Home / Discussions / C#
   

C#

 
GeneralRe: Please help me. don't working save button Pin
thatraja6-Mar-14 3:59
professionalthatraja6-Mar-14 3:59 
GeneralRe: Please help me. don't working save button Pin
bayaa6-Mar-14 16:09
bayaa6-Mar-14 16:09 
GeneralRe: Please help me. don't working save button Pin
bayaa6-Mar-14 16:10
bayaa6-Mar-14 16:10 
AnswerRe: Please help me. don't working save button Pin
Member 104046945-Mar-14 21:18
Member 104046945-Mar-14 21:18 
GeneralRe: Please help me. don't working save button Pin
bayaa6-Mar-14 15:55
bayaa6-Mar-14 15:55 
GeneralRe: Please help me. don't working save button Pin
bayaa6-Mar-14 19:29
bayaa6-Mar-14 19:29 
GeneralRe: Please help me. don't working save button Pin
bayaa6-Mar-14 19:52
bayaa6-Mar-14 19:52 
QuestionInheriting binding list Pin
Gilbert Consellado4-Mar-14 21:52
professionalGilbert Consellado4-Mar-14 21:52 
Hello everyone,

I found that i can use bindinglist as datasource, now when i implement in a griedview datasource i came up with a problem that i cant track which item has been modified. so i make a little research and found about the INotifyPropertyChanged and will trigger if there is data that has been modified.

but what i need is a list of item that has been modified. not triggering an event for every property of the object that has been modified, and also found out that implementing INotifyPropertyChanged can cause some drawback in performance as i read in some blog.

so i came up inheriting bindinglist and add some features, without implementing NotifyPropertyChanged in the item class.

so this is my solution.

First the IEditableObject
C#
namespace gcsc.Interface
{
    public interface IEditableObject
    {
        bool IsNew();

        void IsNew(bool status);

        bool IsDirty();

        void IsDirty(bool status);
    }
}


then the class that should be inherited by the item

C#
namespace gcsc.UI
{
    public abstract class EditableObject : Interface.IEditableObject
    {
        private bool _isNew;
        private bool _isDirty;

        public bool IsNew()
        {
            return _isNew;
        }

        public void IsNew(bool status)
        {
            _isNew = status;
        }

        public bool IsDirty()
        {
            return _isDirty;
        }

        public void IsDirty(bool status)
        {
            _isDirty = status;
        }
    }
}


then that last is the GCSCBindingList<t> that will inherit the binding list

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using GI = gcsc.Interface;

namespace gcsc.Util
{
    public sealed class GCSCBindingList<T> : BindingList<T>
    {
        private readonly List<T> _removeItems;
        private readonly bool _initValues;

        public IEnumerable<T> GetRemovedItems()
        {
            return _removeItems.AsEnumerable();
        }

        public IEnumerable<T> GetUpdatedItems()
        {
            foreach (GI.IEditableObject item in this)
            {
                if (item.IsDirty())
                    yield return (T)item;
            }
        }

        public IEnumerable<T> GetNewItems()
        {
            foreach (GI.IEditableObject item in this)
            {
                if (item.IsNew())
                    yield return (T)item;
            }
        }

        public GCSCBindingList()
        {
            if (!typeof(T).ImplementsInterface<GI.IEditableObject>())
                throw new ArrayTypeMismatchException("T must implement gcsc.Interface.IEditableObject");

            _removeItems = new List<T>();

            AllowEdit = true;
            AllowNew = true;
            AllowRemove = true;
        }

        public GCSCBindingList(IList<T> list)
        {
            if (!typeof(T).ImplementsInterface<GI.IEditableObject>())
                throw new ArrayTypeMismatchException("T must implement gcsc.Interface.IEditableObject");

            _removeItems = new List<T>();

            AllowEdit = true;
            AllowNew = true;
            AllowRemove = true;

            _initValues = true;

            foreach (var v in list)
                base.Add(v);

            _initValues = false;
        }

        protected override void InsertItem(int index, T item)
        {
            if (!_initValues)
                ((GI.IEditableObject)item).IsNew(true);
            base.InsertItem(index, item);
        }

        public new void Insert(int index, T item)
        {
            ((GI.IEditableObject)item).IsNew(true);
            base.Insert(index, item);
        }

        protected override void RemoveItem(int index)
        {
            if (index >= 0 && index < base.Count)
            {
                var item = base[index];
                _removeItems.Add(item);
                base.RemoveItem(index);
            }
        }
    }
}


XML
Now with the GCSCBindingList<T>

i can retrieve all deleted items and all the new items, but still i cant find a way to collect all edited items.

the IsDirty(bool status) method should set to true if the item has been modified, but i cant find the way to implement this.

Could you help me do this?

this is my sample usage, first the class


C#
public class person : gcsc.UI.EditableObject
    {
        public person()
        {
        }

        public person(string lastname, string firstname)
        {
            LastName = lastname;
            FirstName = firstname;
        }

        public override string ToString()
        {
            return string.Format("{0}, {1} status new:{2}, dirty:{3}", LastName, FirstName, IsNew(), IsDirty());
        }

        public string FirstName { get; set; }

        public string LastName { get; set; }
    }


then can be used like this

C#
List<person> p = new List<person>();
p.Add(new person("John", "smith"));
p.Add(new person("pual", "carl"));

GB = new gcsc.Util.GCSCBindingList<person>(p);


C#
private void simpleButton1_Click(object sender, EventArgs e)
        {

            GB.RemoveAt(0);

            foreach (person p in GB.GetNewItems())
            {
                XtraMessageBox.Show(p.ToString());
            }

            foreach (person p in GB.GetRemovedItems())
            {
                XtraMessageBox.Show(p.ToString());
            }

            foreach (person p in GB.GetUpdatedItems())
            {
                MessageBox.Show(p.ToString());
            }
        }


now the GB.GetUpdatedItems() is empty because the IsDirty() is still return false even there is item edited.

Is there way enumerating all edited items?

Please help me solving this problem.

I Will appreciate for any help will come.

Thank you,

PS. sorry for my bad english
AnswerRe: Inheriting binding list Pin
BobJanova5-Mar-14 0:56
BobJanova5-Mar-14 0:56 
GeneralRe: Inheriting binding list Pin
Gilbert Consellado5-Mar-14 22:19
professionalGilbert Consellado5-Mar-14 22:19 
GeneralRe: Inheriting binding list Pin
Gilbert Consellado5-Mar-14 22:42
professionalGilbert Consellado5-Mar-14 22:42 
GeneralRe: Inheriting binding list Pin
BobJanova6-Mar-14 6:10
BobJanova6-Mar-14 6:10 
GeneralRe: Inheriting binding list Pin
Gilbert Consellado6-Mar-14 22:05
professionalGilbert Consellado6-Mar-14 22:05 
AnswerRe: Inheriting binding list Pin
Eddy Vluggen6-Mar-14 7:10
professionalEddy Vluggen6-Mar-14 7:10 
GeneralRe: Inheriting binding list Pin
Gilbert Consellado6-Mar-14 22:25
professionalGilbert Consellado6-Mar-14 22:25 
QuestionIs C# is fully object oriented or not? Pin
Member 102864334-Mar-14 21:48
Member 102864334-Mar-14 21:48 
AnswerRe: Is C# is fully object oriented or not? Pin
Peter Leow4-Mar-14 21:59
professionalPeter Leow4-Mar-14 21:59 
GeneralRe: Is C# is fully object oriented or not? Pin
BobJanova5-Mar-14 0:39
BobJanova5-Mar-14 0:39 
AnswerRe: Is C# is fully object oriented or not? Pin
OriginalGriff4-Mar-14 22:14
mveOriginalGriff4-Mar-14 22:14 
AnswerRe: Is C# is fully object oriented or not? Pin
BillWoodruff4-Mar-14 23:08
professionalBillWoodruff4-Mar-14 23:08 
GeneralRe: Is C# is fully object oriented or not? Pin
harold aptroot5-Mar-14 1:21
harold aptroot5-Mar-14 1:21 
JokeRe: Is C# is fully object oriented or not? PinPopular
Wayne Gaylard5-Mar-14 1:37
professionalWayne Gaylard5-Mar-14 1:37 
GeneralRe: Is C# is fully object oriented or not? Pin
harold aptroot5-Mar-14 1:39
harold aptroot5-Mar-14 1:39 
GeneralRe: Is C# is fully object oriented or not? Pin
OriginalGriff5-Mar-14 1:56
mveOriginalGriff5-Mar-14 1:56 
GeneralRe: Is C# is fully object oriented or not? Pin
Wayne Gaylard5-Mar-14 1:58
professionalWayne Gaylard5-Mar-14 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.