如何在wpf数据网格中获得行索引

本文关键字:索引 网格 数据网 wpf 数据 | 更新日期: 2023-09-27 18:08:32

我有一个数据网格,当我在这个数据网格上加载数据时,我想把重点放在选定的行上。但我没有得到行下标。

if (gridAppointment.SelectedIndex >= 0)
{
   gridAppointment.ScrollIntoView(gridAppointment.Items[gridAppointment.SelectedIndex]);
   DataGridRow row = (DataGridRow)gridAppointment.ItemContainerGenerator.ContainerFromIndex(
         gridAppointment.SelectedIndex);
   if (row != null)
       row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));                                               
 }

如何在wpf数据网格中获得行索引

当您选择行时,下一个条件为真,并且您可以像这样获得所有单元格值。

if (gridAppointment.SelectedIndex >= 0)
{
    DataRowView dataRow = (DataRowView)gridAppointment.SelectedItem;
    int i = dataRow.Row.ItemArray[0].ToString();
    // ItemArray[1] is the next column and so on.
}