BindingList<> Listchanged event not fired
Asked Answered
S

1

6

i have the following code in my application. But Listchanged event is not fired as expected. I have an object "Booking". I am calling this from frmMain. Would you please tell me the problem ??

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.ComponentModel;

namespace CustomObjects
{
public class Booking:ObjectBase
{

    private int pBookingNo=0;
    private BindingList<Loans> pCashLoans = new BindingList<Loans>();

    public int BookingNo
    {
        get { return pBookingNo; }
        set
        {
            if (!value.Equals(pBookingNo))
            {
                pBookingNo = value;
                PropertyHasChanged("BookingNo");
            }
        }
    }

   public BindingList<Loans> CashLoans
    {
        get { return pCashLoans; }
        set 
        { 
            pCashLoans = value;
            //CalculateCashLoan(this,new System.ComponentModel.ListChangedEventArgs(ListChangedType.Reset,-1));
            PropertyHasChanged("CashLoans");
        }
    }

    private decimal pTakenCashLoan = 0;
    public decimal TakenCashLoan
    {
        get { return pTakenCashLoan; }
        set
        {
            pTakenCashLoan = value;
            PropertyHasChanged("TakenCashLoan");
        }
    }

      public void CalculateCashLoan(object sender, ListChangedEventArgs args)
    {
        decimal total = 0;
        foreach (Loans loan in pCashLoans)
        {
            total += loan.LoanAmount;
        }
        this.TakenCashLoan = total;
    }

    public Booking()
    {
        this.pCashLoans.ListChanged += this.CalculateCashLoan;
    }


    public static Booking FillEntity(OleDbDataReader Reader, OleDbConnection Connection)
    {
        Booking booking = new Booking();
        booking.BookingNo = (int)Reader["BookingNo"];

        booking.CashLoans = Loans.GetLoanList(booking.BookingNo, 1, Connection);
        booking.MarkOld();
        return booking;
    }

    public static Booking GetEntity(int bookingNo, string ConnectionString)
    {
        Booking booking =new Booking();
        using (OleDbConnection Connection = new OleDbConnection(ConnectionString))
        {
            string sqlSelect = "SELECT BookingNo FROM tblBooking WHERE BookingNo=" + bookingNo + "";
            using (OleDbCommand cmd = new OleDbCommand(sqlSelect, Connection))
            {
                Connection.Open();
                OleDbDataReader bReader = cmd.ExecuteReader();
                if (bReader.HasRows)
                {
                    bReader.Read();
                    booking = FillEntity(bReader, Connection);
                }
                Connection.Close();

                if (!bReader.IsClosed)
                {
                    bReader.Close();
                }
            }
        }
        return booking;
    }

}

}

I am calling this code from here

    private void frmMain_Load(object sender, EventArgs e)
    {
        AddDataBindings();
        cmbBookingType.DataSource = BookingType.GetSelectionList(ConnectionString.CreateConnectionStringForAccess("LOCAL", "2012"));
    }

    private Booking booking=new Booking();
    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
           booking = Booking.GetEntity(1, ConnectionString.CreateConnectionStringForAccess("LOCAL", "2012"));
            bsBooking.DataSource = booking;
        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message);
            MessageBox.Show(Ex.StackTrace);
        }
    }
Sightseeing answered 28/9, 2012 at 12:3 Comment(0)
T
4

It's because you are assigning a new BindingList instance to the property instead of adding and removing items to the existing list.

Try making the property CashLoans read only, i.e. remove the set accessor and modify the code to clear the list and add the new items.

Tuscan answered 28/9, 2012 at 12:48 Comment(1)
Dear Malio, many many maaaaaaany thanks. I have got the clue and redefined the code as follows: public BindingList<Loan> CashLoans { get { return pCashLoans; } set { foreach (Loan loan in value) { pCashLoans.Add(loan); } } } And its working great as expected.Sightseeing

© 2022 - 2024 — McMap. All rights reserved.