RowEditEnding事件获取空值

时间:2013-08-28 01:54:23

标签: c# wpf entity-framework wpfdatagrid ilist

好在C# .NET 4.5并使用DataGrid

我的数据网格绑定到ADO.Net DataEntity。现在,如果我突出显示Datagrid中的现有行并点击我执行以下代码的按钮:IList rows = DataGrid.SelectedItems;我可以单步执行代码并查看IList是否填充了正确的值。

我的数据网格在XAML中绑定为ItemsSource="{Binding}",并且数据网格中的每一列都绑定到实体中的列,如:Binding="{Binding Path=InventoryName, Mode=TwoWay}"

在后面的代码中,我使用LINQ查询将其设置为实体框架,如下所示:

var fillList = (from g in MyDataEntities.Table1
            where g.OnLIst == true
            orderby g.InventoryName
            select g);
DataGrid.ItemsSource = fillList.ToList(); 

我需要做的是双重的:在RowEditEnding事件中,使用行中的值填充IList。填充后,我需要访问IList中的各个条目。

通过测试和单步执行代码,我认为我的问题是当我输入一个空行时,它在RowEditEnding事件被触发时没有更新实体。我认为这是因为当我单步执行代码并展开IList时,其中的所有值都为null。在填写IList之前,是否必须执行某些操作来更新实体?

一旦填好,我怎样才能访问各个值?当我尝试使用foreach语句填充变量时,所有它都将值显示为:Namespace.TableName

1 个答案:

答案 0 :(得分:0)

试试这个,这可能对你的问题有帮助。

List<Table1> TestCollection = new List<Table1>();

public void BindData()
{
     TestCollection = MyDataEntities.Table1.Where(x=>x.OnLIst == true).ToList();
     DataGrid.ItemsSource = TestCollection;
}

然后您可以在Datagrid上逐个访问您的数据。

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
     Table1 NewTable = TestCollection[DataGrid.SelectedIndex];
}
相关问题