[英]How to insert items in a List
In the coding below I am inserting items into a datagrid
from a combobox
. 在下面的代码中,我将项目从
combobox
插入到datagrid
。
private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (TruckServiceClient TSC = new TruckServiceClient())
{
var item = cmbAddExtras.SelectedItem as ExtraDisplayItems;
if (item != null)
{
var displayItem = new List<ExtraDisplayItems>
{
new ExtraDisplayItems
{
ItemId = item.ItemId,
ItemCode = item.ItemCode,
ItemDescription = item.ItemDescription,
ItemSellingPrice = item.ItemSellingPrice,
displayItems = item.displayItems //Always null?
}
};
dgAddExtras.Items.Add(item);
}
}
btnRemoveAllExtras.Visibility = Visibility.Visible;
}
I've created a variable in my class below, where I want to be able to access the items in a different method and get the Sum total of my ItemSellingPrice . 我在下面的类中创建了一个变量,希望在该变量中使用不同的方法访问这些项,并获取ItemSellingPrice的总和 。
My class: 我的课:
public class ExtraDisplayItems
{
public List<ExtraDisplayItems> displayItems;
public int ItemId { get; set; }
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
public double? ItemSellingPrice { get; set; }
}
Now my issue is that in the top method where I am inserting the items into the datagrid, my displayItems
variable is always null for some reason. 现在我的问题是,在将项目插入到datagrid中的top方法中,由于某种原因,我的
displayItems
变量始终为null 。 Is there some special way that I need to load the items into the displayItems
list in my class? 是否需要一些特殊的方式将项目加载到班级的
displayItems
列表中?
You don't need to store the whole collection of selected items on each item you add to the DataGrid. 您无需在添加到DataGrid的每个项目上存储选定项目的整个集合。 You can retrieve the collection from the DataGrid itself, and you can do so using a calculated property like this, for instance (you may need to add
System.Linq
to your usings): 您可以从DataGrid本身检索集合,并且可以使用这样的计算属性来实现(例如,您可能需要将
System.Linq
添加到使用中):
private IEnumerable<ExtraDisplayItems> SelectedDisplayItems
{
get
{
return dgAddExtras.Items.Cast<ExtraDisplayItems>();
}
}
That way, you can remove the list from the ExtraDisplayItems
class. 这样,您可以从
ExtraDisplayItems
类中删除列表。
public class ExtraDisplayItems
{
public int ItemId { get; set; }
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
public double? ItemSellingPrice { get; set; }
}
Your SelectionChanged
method would end up like this: 您的
SelectionChanged
方法最终将像这样:
private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// You're not using TSC, so you don't need this either
//using (TruckServiceClient TSC = new TruckServiceClient())
//{
var item = cmbAddExtras.SelectedItem as ExtraDisplayItems;
if (item != null)
{
dgAddExtras.Items.Add(item);
}
//}
btnRemoveAllExtras.Visibility = Visibility.Visible;
}
And in your other method that needs to calculate the sum of ItemSellingPrice
, you just need to use the calculated property. 在需要计算
ItemSellingPrice
之和的其他方法中,您只需要使用计算出的属性即可。
private void YourOtherMethod()
{
// do stuff
var sum = SelectedDisplayItems.Sum(item => item.ItemSellingPrice ?? 0); // Since ItemSellingPrice can be null, use 0 instead
// do more stuff
}
cmbAddExtras.SelectedItem
is not of type ExtraDisplayItems
. cmbAddExtras.SelectedItem
的类型不是ExtraDisplayItems
。 It is either null or a different type 它为null或其他类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.