BindingList의 ItemChanging 이벤트에서 삭제 된 항목 가져 오기

메가 마인드

내 응용 프로그램에서 ItemChanged 이벤트와 함께 Binding List를 사용하고 있습니다.

ItemChanged 이벤트에서 속성의 이전 값을 알 수있는 방법이 있습니까? 현재이를 달성하기 위해 'OldValue'라는 별도의 속성을 추가하고 있습니다.

항목 변경 이벤트에서 삭제 된 항목에 대해 알 수있는 방법이 있습니까? 목록에서 어떤 항목이 삭제되었는지 알 수있는 방법을 찾을 수 없습니다.

ㅏ''

올바르게 이해했다면 바인딩 목록에서 삭제 된 항목에 대한 정보를 얻고 싶습니다.

가장 쉬운 방법은 바인딩 목록에서 파생되는 바인딩 목록을 만드는 것입니다.

내부에는 RemoveItem 메서드가 재정의되므로 바인딩 목록에서 항목을 제거하기 전에 제거 할 항목이 포함 된 이벤트를 실행할 수 있습니다.

public class myBindingList<myInt> : BindingList<myInt>
{
    protected override void RemoveItem(int itemIndex)
    {
        //itemIndex = index of item which is going to be removed
        //get item from binding list at itemIndex position
        myInt deletedItem = this.Items[itemIndex];

        if (BeforeRemove != null)
        {
            //raise event containing item which is going to be removed
            BeforeRemove(deletedItem);
        }

        //remove item from list
        base.RemoveItem(itemIndex);
    }

    public delegate void myIntDelegate(myInt deletedItem);
    public event myIntDelegate BeforeRemove;
}

예를 들어, INotifyPropertyChanged를 구현하는 myInt 유형을 만들었습니다. 인터페이스는 바인딩 목록에서 요소를 추가 / 삭제 한 후 dataGridView를 새로 고치는 것입니다.

public class myInt : INotifyPropertyChanged
{
    public myInt(int myIntVal)
    {
        myIntProp = myIntVal;
    }
    private int iMyInt;
    public int myIntProp {
        get
        {
            return iMyInt;
        }
        set
        {
            iMyInt = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("myIntProp"));
            }
        } 
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

ints (정확히 myInts)로 바인딩 목록을 초기화 한 다음 목록을 dataGridView (프레젠테이션 목적)에 바인딩하고 BeforeRemove 이벤트를 구독합니다.

bindingList = new myBindingList<myInt>();
bindingList.Add(new myInt(8));
bindingList.Add(new myInt(9));
bindingList.Add(new myInt(11));
bindingList.Add(new myInt(12));

dataGridView1.DataSource = bindingList;
bindingList.BeforeRemove += bindingList_BeforeRemove;

BeforeRemove 이벤트가 발생하면 삭제 된 항목이 있습니다.

void bindingList_BeforeRemove(Form1.myInt deletedItem)
{
    MessageBox.Show("You've just deleted item with value " + deletedItem.myIntProp.ToString());
}

아래는 전체 예제 코드입니다 (폼에 버튼 3 개와 dataGridView 드롭)-버튼 1은 바인딩 목록을 초기화하고 버튼 2는 항목을 목록에 추가하고 버튼 3은 입찰 목록에서 항목을 제거합니다.

삭제하기 전에

삭제 후

삭제됨

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bindinglist
{
    public partial class Form1 : Form
    {
        myBindingList<myInt> bindingList;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bindingList = new myBindingList<myInt>();
            bindingList.Add(new myInt(8));
            bindingList.Add(new myInt(9));
            bindingList.Add(new myInt(11));
            bindingList.Add(new myInt(12));

            dataGridView1.DataSource = bindingList;
            bindingList.BeforeRemove += bindingList_BeforeRemove;
        }

        void bindingList_BeforeRemove(Form1.myInt deletedItem)
        {
            MessageBox.Show("You've just deleted item with value " + deletedItem.myIntProp.ToString());
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            bindingList.Add(new myInt(13));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bindingList.RemoveAt(dataGridView1.SelectedRows[0].Index);
        }

        public class myInt : INotifyPropertyChanged
        {
            public myInt(int myIntVal)
            {
                myIntProp = myIntVal;
            }
            private int iMyInt;
            public int myIntProp {
                get
                {
                    return iMyInt;
                }
                set
                {
                    iMyInt = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("myIntProp"));
                    }
                } 
            }

            public event PropertyChangedEventHandler PropertyChanged;
        }

        public class myBindingList<myInt> : BindingList<myInt>
        {
            protected override void RemoveItem(int itemIndex)
            {
                myInt deletedItem = this.Items[itemIndex];

                if (BeforeRemove != null)
                {
                    BeforeRemove(deletedItem);
                }

                base.RemoveItem(itemIndex);
            }

            public delegate void myIntDelegate(myInt deletedItem);
            public event myIntDelegate BeforeRemove;
        }
    }
}

의견에 대한 답변

"질문의 다른 부분은 => 목록에서 변경된 항목의 이전 값을 알 수있는 방법이 있습니까? ListChangedEvent에서 아무것도 공유하지 않습니다."

항목의 이전 값을 보려면 SetItem 메서드를 재정의 할 수 있습니다.

protected override void SetItem(int index, myInt item)
{
    //here we still have old value at index
    myInt oldMyInt = this.Items[index];
    //new value
    myInt newMyInt = item;

    if (myIntOldNew != null)
    {
        //raise event
        myIntOldNew(oldMyInt, newMyInt);
    }

    //update item at index position
    base.SetItem(index, item);
}

지정된 인덱스의 객체가 다음과 같이 변경되면 발생합니다.

bindingList[dataGridView1.SelectedRows[0].Index] = new myInt(new Random().Next());

까다로운 부분은 항목의 속성을 직접 수정하려고하면

bindingList[dataGridView1.SelectedRows[0].Index].myIntProp = new Random().Next();

SetItem 은 실행되지 않으며 전체 개체가 교체되어야합니다.

따라서이를 처리하려면 다른 델리게이트 및 이벤트가 필요합니다.

public delegate void myIntDelegateChanged(myInt oldItem, myInt newItem);
public event myIntDelegateChanged myIntOldNew;

그러면 우리는 이것을 구독 할 수 있습니다

bindingList.myIntOldNew += bindingList_myIntOldNew;

그리고 그것을 처리

void bindingList_myIntOldNew(Form1.myInt oldItem, Form1.myInt newItem)
{
    MessageBox.Show("You've just CHANGED item with value " + oldItem.myIntProp.ToString() + " to " + newItem.myIntProp.ToString());
}

전에 이벤트 발생 변경

코드 업데이트 (4 개 버튼 필요, 4 번째 선택 항목 수정)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bindinglist
{
    public partial class Form1 : Form
    {
        myBindingList<myInt> bindingList;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bindingList = new myBindingList<myInt>();
            bindingList.Add(new myInt(8));
            bindingList.Add(new myInt(9));
            bindingList.Add(new myInt(11));
            bindingList.Add(new myInt(12));

            dataGridView1.DataSource = bindingList;
            bindingList.BeforeRemove += bindingList_BeforeRemove;
            bindingList.myIntOldNew += bindingList_myIntOldNew;
        }

        void bindingList_myIntOldNew(Form1.myInt oldItem, Form1.myInt newItem)
        {
            MessageBox.Show("You've just CHANGED item with value " + oldItem.myIntProp.ToString() + " to " + newItem.myIntProp.ToString());
        }

        void bindingList_BeforeRemove(Form1.myInt deletedItem)
        {
            MessageBox.Show("You've just deleted item with value " + deletedItem.myIntProp.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bindingList.Add(new myInt(13));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bindingList.RemoveAt(dataGridView1.SelectedRows[0].Index);
        }

        public class myInt : INotifyPropertyChanged
        {
            public myInt(int myIntVal)
            {
                myIntProp = myIntVal;
            }
            private int iMyInt;
            public int myIntProp {
                get
                {
                    return iMyInt;
                }
                set
                {
                    iMyInt = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("myIntProp"));
                    }
                } 
            }
            
            public event PropertyChangedEventHandler PropertyChanged;
        }

        public class myBindingList<myInt> : BindingList<myInt>
        {
            protected override void SetItem(int index, myInt item)
            {
                myInt oldMyInt = this.Items[index];
                myInt newMyInt = item;

                if (myIntOldNew != null)
                {
                    myIntOldNew(oldMyInt, newMyInt);
                }

                base.SetItem(index, item);
            }
            
            protected override void RemoveItem(int itemIndex)
            {
                myInt deletedItem = this.Items[itemIndex];

                if (BeforeRemove != null)
                {
                    BeforeRemove(deletedItem);
                }

                base.RemoveItem(itemIndex);
            }

            public delegate void myIntDelegateChanged(myInt oldItem, myInt newItem);
            public event myIntDelegateChanged myIntOldNew;

            public delegate void myIntDelegate(myInt deletedItem);
            public event myIntDelegate BeforeRemove;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            bindingList[dataGridView1.SelectedRows[0].Index] = new myInt(new Random().Next());
        }
    }
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

onDelete 함수에서 삭제 된 항목의 값 가져 오기

분류에서Dev

SelectionChanged 이벤트에서 GridView의 항목 위치 가져 오기

분류에서Dev

Firestore에서 삭제 된 문서의 ID 가져 오기

분류에서Dev

Cloud Datastore의 지정된 키에서 항목 가져 오기

분류에서Dev

XSLT : "토큰 화 된 항목"의 요소 이름 가져 오기

분류에서Dev

Sitecore 항목 삭제에 대한 전체 경로 가져 오기

분류에서Dev

Flutter / Firebase의 여러 문서에서 연결된 항목 목록 가져 오기

분류에서Dev

이벤트 추적 (ETW) 세션에서 특정 프로세스에 의해 생성 / 수정 / 삭제 된 파일 가져 오기

분류에서Dev

Python : 이전에 추가 된 항목과의 차이가 적을 때 목록에서 항목 삭제

분류에서Dev

Facebook에서 사용자의 이벤트 목록 가져 오기

분류에서Dev

(날짜) 목록의 (이벤트) 목록에서 항목 삭제

분류에서Dev

xlrd 값 가져 오기, 서식 지우기, 정수가 아닌 항목 삭제

분류에서Dev

jstree의 새 div에 드롭 된 항목의 이름 가져 오기

분류에서Dev

보류 이벤트의 Windows Phone 목록 상자에서 선택한 항목 가져 오기

분류에서Dev

목록 목록에서 고유 요소의 제한된 목록 가져 오기

분류에서Dev

ListBox의 항목 높이 가져 오기

분류에서Dev

Kotlin의 Exposed에서 삭제 된 행 수를 가져 오나요?

분류에서Dev

Python (수프) : 중첩 된 데이터 가져 오기 및 태그의 마지막 항목 가져 오기

분류에서Dev

MySQL Inner Join 조인 된 테이블의 마지막 항목 가져 오기

분류에서Dev

rm -rf로 삭제 된 파일 목록 가져 오기

분류에서Dev

'find -delete'로 삭제 된 파일 목록 가져 오기

분류에서Dev

타임 스탬프별로 정렬 된 테이블에서 지정된 수량의 항목 가져 오기

분류에서Dev

목록의 각 항목에 대한 개체 컬렉션 (제품) 가져 오기

분류에서Dev

예정된 이벤트에 대한 가져 오기 문제

분류에서Dev

item : deleted 이벤트에서 Sitecore 항목의 상위 항목을 가져 오는 방법은 무엇입니까?

분류에서Dev

SharePoint 목록에서 오늘의 항목 가져 오기

분류에서Dev

Vuetify 데이터 테이블에서 필터링 된 항목의 길이 가져 오기

분류에서Dev

목록보기의 항목 명령 이벤트가 복합 제어에서 발생하지 않습니다.

분류에서Dev

Django DeleteView에서 삭제 된 개체 가져 오기

Related 관련 기사

  1. 1

    onDelete 함수에서 삭제 된 항목의 값 가져 오기

  2. 2

    SelectionChanged 이벤트에서 GridView의 항목 위치 가져 오기

  3. 3

    Firestore에서 삭제 된 문서의 ID 가져 오기

  4. 4

    Cloud Datastore의 지정된 키에서 항목 가져 오기

  5. 5

    XSLT : "토큰 화 된 항목"의 요소 이름 가져 오기

  6. 6

    Sitecore 항목 삭제에 대한 전체 경로 가져 오기

  7. 7

    Flutter / Firebase의 여러 문서에서 연결된 항목 목록 가져 오기

  8. 8

    이벤트 추적 (ETW) 세션에서 특정 프로세스에 의해 생성 / 수정 / 삭제 된 파일 가져 오기

  9. 9

    Python : 이전에 추가 된 항목과의 차이가 적을 때 목록에서 항목 삭제

  10. 10

    Facebook에서 사용자의 이벤트 목록 가져 오기

  11. 11

    (날짜) 목록의 (이벤트) 목록에서 항목 삭제

  12. 12

    xlrd 값 가져 오기, 서식 지우기, 정수가 아닌 항목 삭제

  13. 13

    jstree의 새 div에 드롭 된 항목의 이름 가져 오기

  14. 14

    보류 이벤트의 Windows Phone 목록 상자에서 선택한 항목 가져 오기

  15. 15

    목록 목록에서 고유 요소의 제한된 목록 가져 오기

  16. 16

    ListBox의 항목 높이 가져 오기

  17. 17

    Kotlin의 Exposed에서 삭제 된 행 수를 가져 오나요?

  18. 18

    Python (수프) : 중첩 된 데이터 가져 오기 및 태그의 마지막 항목 가져 오기

  19. 19

    MySQL Inner Join 조인 된 테이블의 마지막 항목 가져 오기

  20. 20

    rm -rf로 삭제 된 파일 목록 가져 오기

  21. 21

    'find -delete'로 삭제 된 파일 목록 가져 오기

  22. 22

    타임 스탬프별로 정렬 된 테이블에서 지정된 수량의 항목 가져 오기

  23. 23

    목록의 각 항목에 대한 개체 컬렉션 (제품) 가져 오기

  24. 24

    예정된 이벤트에 대한 가져 오기 문제

  25. 25

    item : deleted 이벤트에서 Sitecore 항목의 상위 항목을 가져 오는 방법은 무엇입니까?

  26. 26

    SharePoint 목록에서 오늘의 항목 가져 오기

  27. 27

    Vuetify 데이터 테이블에서 필터링 된 항목의 길이 가져 오기

  28. 28

    목록보기의 항목 명령 이벤트가 복합 제어에서 발생하지 않습니다.

  29. 29

    Django DeleteView에서 삭제 된 개체 가져 오기

뜨겁다태그

보관