简体   繁体   English

如何在WPF DataGrid中按索引获取行

[英]How to get row by index in wpf datagrid

I have a datagrid and when I load data on this data grid I want to focus on selected row. 我有一个数据网格,当我在此数据网格上加载数据时,我要关注选定的行。 But I don't get row by index. 但我没有得到索引。

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));                                               
 }

When you select the row, the next condition is true and you are able to get all the cells values like this. 当选择该行时,下一个条件为true,这样便可以获取所有像元值。

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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM