2

好的,所以在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 时,其中的所有值都是空的。在填写 IList 之前,我是否必须做一些事情来更新实体?

填充后如何访问各个值?当我尝试使用 foreach 语句填充变量时,它的所有值都显示为:Namespace.TableName

4

1 回答 1

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];
}
于 2014-04-08T02:58:41.003 回答