RowEditEnding事件获取null值
本文关键字:null 获取 事件 RowEditEnding | 更新日期: 2023-09-27 18:29:11
在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事件中,用行中的vales填充IList。填充后,我需要访问IList中的各个条目。
通过测试和逐步执行代码,我认为我的问题是,当我输入一个空行时,当RowEditEnding事件触发时,它没有更新实体。我认为这是因为当我遍历代码并展开IList时,其中的所有值都为null。在填写IList之前,我是否必须做一些事情来更新实体?
一旦填写完毕,我如何访问个人价值观?当我尝试使用foreach语句填充变量时,它显示的值为:Namespace.TableName
试试这个,它可能会对你的问题有所帮助。
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];
}