好的,所以在 C#
.NET 4.5
中使用 DataGrid
我将数据网格绑定(bind)到 ADO.Net DataEntity
。现在,如果我突出显示 Datagrid 中的现有行并点击一个按钮,我将执行以下代码:IList rows = DataGrid.SelectedItems;
我可以单步执行代码并看到 IList 填充适当的值。
我的数据网格在 XAML 中绑定(bind)为 ItemsSource="{Binding}"
并且数据网格中的每一列都绑定(bind)到实体中的一列,如下所示:Binding="{Binding Path=InventoryName, Mode=TwoWay}"
在后面的代码中,我使用 LINQ 查询对 Entity Framework 进行设置,如下所示:
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 时,其中的所有值都为空。在填充 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];
}
关于c# - RowEditEnding 事件获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18478217/