DataGrid foreach实体错误

时间:2015-08-22 17:05:41

标签: c# datagrid visual-studio-lightswitch lightswitch-2012

在这个_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);
            }
        }
    }

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

  

无法转换类型&#39; Microsoft.LightSwitch.Presentation.Framework.NewItemPlaceholderDataContext&#39;输入&#39; LightSwitchApplication.InsuranceQuotation&#39;。

1 个答案:

答案 0 :(得分:1)

尝试此修复

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