更新WPF中数据集的视图

时间:2016-05-30 07:59:12

标签: c# wpf

我在WPF表单上有一个数据源,其形状为我的表的详细视图。当我搜索特定行时,我希望它在绑定的文本框中更新,但它们只显示空白。

        System.Windows.Data.CollectionViewSource employeesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("employeesViewSource")));
        Yello.YelloDataSet yelloDataSet = ((Yello.YelloDataSet)(this.FindResource("yelloDataSet")));
        var adapter = new YelloDataSetTableAdapters.EmployeesTableAdapter();

        var row = yelloDataSet.Employees.Select("ID='11'");
        employeesViewSource.View.MoveCurrentTo(row);

如果我例如:

        System.Windows.Data.CollectionViewSource employeesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("employeesViewSource")));
        employeesViewSource.View.MoveCurrentToNext();

它完美运行并使用绑定进行更新。我使用了错误的功能还是我错过了什么?

1 个答案:

答案 0 :(得分:1)

我认为发生的事情是您正在从yelloDataSet中选择一行,之后您尝试移动到employeesViewSource中的那一行,但问题是这些行不一样,虽然数据可能是。要移至CollectionViewSource中的某一行,您必须在集合中搜索它。所以你会做这样的事情:

var row = employeesViewSource.View.OfType<DataSet>().Select("ID='11'");
employeesViewSource.View.MoveCurrentTo(row);

<击>

修改

好的,我没有实际尝试代码就回答了,我错了。尝试下一个代码,我认为它应该适合你:

var row = employeesViewSource.View.OfType<DataRowView>()
          .Where(x => x.Row.Field<string>("ID") == "11")
          .FirstOrDefault();
employeesViewSource.View.MoveCurrentTo(row);

但这取决于CollectionViewSource的类型。我用DataTable尝试过它。例如,如果它是DataSet,它可能会改变。如果这不起作用,请告诉我源的类型......