的BindingList<> Listchanged事件未被触发

时间:2012-09-28 12:03:36

标签: c# bindinglist

我的申请中有以下代码。但Listchanged事件未按预期触发。我有一个“预订”对象。我是从frmMain打电话给我的。你能告诉我这个问题吗?

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;
    }

}

}

我从这里调用此代码

    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);
        }
    }

1 个答案:

答案 0 :(得分:4)

这是因为您要为属性分配新的BindingList实例,而不是在现有列表中添加和删除项目。

尝试将属性CashLoans设为只读,即删除设置的访问者并修改代码以清除列表并添加新项目。