BindingList Listchanged事件未触发 [英] BindingList Listchanged event not fired

查看:115
本文介绍了BindingList Listchanged事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中包含以下代码.但是Listchanged事件未按预期触发.我有一个对象预订".我是从frmMain调用的.你能告诉我问题吗?

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

推荐答案

您希望在哪里触发ListChanged?

您正在覆盖FillEntity中的CashLoans,因此在构造函数中分配给它的所有内容都消失了.您可能要在FillEntity中附加事件处理程序.
Where would you expect the ListChanged to be fired?

You are overwriting CashLoans in FillEntity, so whatever you assign to it in the constructor is gone. You probably want to attach the event handler in FillEntity.


这篇关于BindingList Listchanged事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆