gpt4 book ai didi

c# - DataGrid foreach 实体错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:01:26 25 4
gpt4 key购买 nike

在这个 _ControlAvailable 中我遇到了一个错误。在我的实体“InsuranceQuotation c”(这是我的 db GridView)中,我可以访问“c.mYear”,这是一个包含日期的列。这来自 db 给我这样的“InsuranceQuotation.mYear”:

void EditableCustomersGrid_ControlAvailable(object sender, ControlAvailableEventArgs e)
{
List<int> ageList = new List<int>();
if (e.Control is DataGrid)
{
DataGrid dg = (DataGrid)e.Control;

// error here "InsuranceQuotation c"
foreach (InsuranceQuotation c in dg.ItemsSource)
{
var yom = c.mYear;
var bday = Convert.ToDateTime(yom);

DateTime today = DateTime.Today;
int age = today.Year - bday.Year;
if (bday > today.AddYears(-age)) age--;

ageList.Add(age);
}
}
}

我在运行时遇到的错误是:

Unable to cast object of type 'Microsoft.LightSwitch.Presentation.Framework.NewItemPlaceholderDataContext' to type 'LightSwitchApplication.InsuranceQuotation'.

最佳答案

试试这个修复

void EditableCustomersGrid_ControlAvailable(object sender, ControlAvailableEventArgs e)
{
List<int> ageList = new List<int>();
if (e.Control is DataGrid)
{
DataGrid dg = (DataGrid)e.Control;
IList rows = dg.ItemsSource.ToList();//this line lets you get the datagrid rows in Ilist
foreach (DataRowView c in rows )
{
var yom = c["mYear"];
var bday = Convert.ToDateTime(yom);
DateTime today = DateTime.Today;
int age = today.Year - bday.Year;
if (bday > today.AddYears(-age)) age--;
ageList.Add(age);
}
}
}

关于c# - DataGrid foreach 实体错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com