【问题标题】:Select a Row in DataGrid and get Values from it with the Row Index在 DataGrid 中选择一行并使用行索引从中获取值
【发布时间】:2015-03-25 18:58:16
【问题描述】:

我有一个包含大约 500 个项目的 DataGrid。用户可以加载更多项目,所以我更改了 DataGrid.ItemsSource。发生这种情况时,我失去了对选定行的关注,但我之前获得了行索引。

现在如何将焦点设置在具有索引的行上?

我在 EF6.1 中使用 WPF 和 C#

代码示例:

<DataGrid Name="MainDataGrid" SelectionChanged="MainDataGrid_SelectionChanged"                      SelectionMode="Single" IsSynchronizedWithCurrentItem="True">
    <DataGrid.Columns>
         <DataGridTextColumn Header="Date" Binding="{Binding Date}"></DataGridTextColumn>
                //...
    </DataGrid.Columns>    
</DataGrid>

【问题讨论】:

  • 如何将数据绑定到数据网格?你能提供你正在做什么的示例代码吗?
  • 你不能使用 myGrid.CurrentRowIndex = SavedIndex 吗?

标签: c# wpf datagrid


【解决方案1】:

您可以通过分配以编程方式执行此操作 数据网格的SelectedIndex 属性的索引值:

MainDataGrid.SelectedIndex = yourIndex;

此外,您可能希望将数据网格滚动到索引处的项目:

MainDataGrid.ScrollIntoView(MainDataGrid.Items[yourIndex]);

看看this的问题可能会很有趣。

【讨论】:

  • @J.Doe 究竟是什么不起作用?鉴于您提供的代码,很难猜出您在做什么。您可能需要提供更多代码,然后才能有人为您指出正确的方向。
  • 我复制了你的代码,它应该在那里。我调试它以检查它现在是否运行良好,但它没有。
猜你喜欢
  • 1970-01-01
  • 2011-04-28
  • 2020-02-12
  • 2016-03-28
  • 2012-11-11
  • 1970-01-01
  • 2015-01-23
  • 2012-08-11
  • 2014-02-20
相关资源
最近更新 更多