How to set focus on a particular row in a datagrid/gridview?

前端 未结 6 1552
清酒与你
清酒与你 2021-01-04 18:07

I have a datagrid/gridview. I\'m populating the grid with 10 rows initially. On a button click every time,I\'m keeping on adding 10 rows to the datagrid/gridview. Now I want

6条回答
  •  Happy的楠姐
    2021-01-04 18:53

    Try this, it's work for me

       public static void ItemSetFocus(DataGrid Dg, int SelIndex)
        {
            if (Dg.Items.Count >= 1 && SelIndex < Dg.Items.Count)
            {
               Dg.ScrollIntoView(Dg.Items.GetItemAt(SelIndex));
               Dg.SelectionMode = DataGridSelectionMode.Single;
               Dg.SelectionUnit = DataGridSelectionUnit.FullRow;
               Dg.SelectedIndex = SelIndex;
               DataGridRow row = (DataGridRow)Dg.ItemContainerGenerator.ContainerFromIndex(SelIndex);
                    row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }
    

提交回复
热议问题