如何将可观察集合放入列表中
我想将当前的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来 inv._RoomPrice 或 coll.RoomNumber 为空。
Looks like inv._RoomPrice or coll.RoomNumber is null.
在抛出异常的行上设置一个断点,并检查每个可能为 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.