c# - 如何使用 Linq 实现 NextRcord 和 PreviousRecord 按钮?

标签 c# .net wpf linq visual-studio

我在下面有这个简单的 WPF 窗口,文本框绑定(bind)到 XAML 中名为 Students 的表中的 StudentName 列。我正在使用 EntityFramework 5 进行数据访问。

现在您在运行它时会看到这个窗口,我该如何实现 NextPrevious 按钮?我在网上看过并建议分页,但是否没有一种简单的单行方式,类似于将记录集移动到新行的老式方式?我不想使用数据网格,我需要稍后在屏幕上显示一堆文本框,并且能够按下一个和上一个进行浏览。

即使没有单行方式(而且我认为没有),我该如何实现 Next 和 Previous 功能?

谢谢你:)

enter image description here

这是我的代码隐藏

        public LinqNext()
        {
            InitializeComponent();
            SchoolEntities schoolEntities = new SchoolEntities();
            MyGrid.DataContext = schoolEntities.Students.ToList();
        }

        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            //What do put here to move to next record
            //(Note this code is obviously "MVVM-FREE"
            //Right now i'd just like to move to next record
        }

这是我的 XAML(简化版)

<Grid x:Name="MyGrid">
        <Button x:Name="NextButton" Content="Next" />
        <Button x:Name="PreviousButton" Content="Previous"/>
        <TextBox x:Name="StudentName" Text="{Binding Path=StudentName}" />
    </Grid>

最佳答案

首先你需要保留当前的学号,然后:

  var next = schoolEntities.Students.FirstOrDefault(x => x.Id > currentId);
  this.StudentName.Text = next.StudentName;

关于c# - 如何使用 Linq 实现 NextRcord 和 PreviousRecord 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17639454/

相关文章:

c# - 方法未找到 : 'System.Object Microsoft.EntityFrameworkCore.Infrastructure.IAnnotatable.get_Item(System.String)'

wpf - 在程序退出之前更新依赖属性

WPF智能UI反模式: How much logic processing is appropriate in XAML?

wpf - 基类 `Application` 在一种解决方案中未被识别为类型

c# - 供手动用户使用的免费 G​​UI 测试器

c# - 从 Web 服务 XML 响应中提取键值对

c# - 删除尾随零

c# - 为什么 .net 在引发 KeyNotFound 异常时不向我们提供 key (我怎样才能得到它?)

javascript - 如何在 ASP Web 表单中通过 pdf.js 打印 pdf

c# - 使用 ServiceStack.Text 解析 JSON 时出现问题