如何将可观察集合放入列表中

发布于 2024-11-26 17:58:41 字数 1308 浏览 1 评论 0原文

我想将当前的 observablecollection 数据放入按钮单击事件的列表中。下面是 C# 代码,但它给出错误:对象引用未设置到对象的实例。

    ObservableCollection<CheckInData> _CheckInCollection = new ObservableCollection<CheckInData>();


    public ObservableCollection<CheckInData> CheckInCollection
    {
        get { return _CheckInCollection; }
    }

    public class CheckInData
    {
        public string RoomNumber { get; set; }
        public decimal Price { get; set; }
        public string Currecny { get; set; }
        public decimal Discount { get; set; }
        public string CheckOut { get; set; }
        public int TotalDay { get; set; }
        public decimal TotalPrice { get; set; }
        public int CheckOutYear { get; set; }
        public int CheckOutMonth { get; set; }
        public int CheckOutDay { get; set; }
        public Boolean IncToday { get; set; }
    }

    private void btnPrintInvoice_Click(object sender, RoutedEventArgs e)
    {
        DataToExcel.Invoice inv = new DataToExcel.Invoice();
        inv._BilledTo = Guest[0];

        foreach (CheckInData coll in _CheckInCollection)
        {

            for (int i = 0; i < _CheckInCollection.Count; i++)
            {
                inv._RoomPrice.Add(coll.RoomNumber[i].ToString());
            }
        }
    }

i want to put current observablecollection data in list on button click event. bellow is c# code but it gives error : Object reference not set to an instance of an object.

    ObservableCollection<CheckInData> _CheckInCollection = new ObservableCollection<CheckInData>();


    public ObservableCollection<CheckInData> CheckInCollection
    {
        get { return _CheckInCollection; }
    }

    public class CheckInData
    {
        public string RoomNumber { get; set; }
        public decimal Price { get; set; }
        public string Currecny { get; set; }
        public decimal Discount { get; set; }
        public string CheckOut { get; set; }
        public int TotalDay { get; set; }
        public decimal TotalPrice { get; set; }
        public int CheckOutYear { get; set; }
        public int CheckOutMonth { get; set; }
        public int CheckOutDay { get; set; }
        public Boolean IncToday { get; set; }
    }

    private void btnPrintInvoice_Click(object sender, RoutedEventArgs e)
    {
        DataToExcel.Invoice inv = new DataToExcel.Invoice();
        inv._BilledTo = Guest[0];

        foreach (CheckInData coll in _CheckInCollection)
        {

            for (int i = 0; i < _CheckInCollection.Count; i++)
            {
                inv._RoomPrice.Add(coll.RoomNumber[i].ToString());
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半透明的墙 2024-12-03 17:58:41

看起来 inv._RoomPrice 或 coll.RoomNumber 为空。

Looks like inv._RoomPrice or coll.RoomNumber is null.

街道布景 2024-12-03 17:58:41

在抛出异常的行上设置一个断点,并检查每个可能为 null 的变量:inv、inv._RoomPrice、coll、coll.RoomNumber

一旦找到它,确定您认为应该在哪里初始化它并修复导致的错误它没有被初始化。

Set a breakpoint on the line that's throwing the exception and inspect each variable that can be null: inv, inv._RoomPrice, coll, coll.RoomNumber

Once you find it, determine where you think it's supposed to be initialized and fix the bug that's causing it not be initialized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文