ksvimal
ksvimal

Reputation: 297

How to change the focus from one cell to the another cell on the next row and the same column

I want to change the row selection from one row to next row and the focus from one cell to the another cell on the the same column.

that is, 2nd row is selected in my datagrid and the focus is currently on the 2nd row 3rd column.

when i click a button on my screen, 3rd must get selected and the focus must be on the 3rd row 3rd column.

now if i type something then it must automatically reflect on the 3rd row 3rd column.

Thanks in advance!

Upvotes: 0

Views: 1701

Answers (2)

ksvimal
ksvimal

Reputation: 297

The following code will solve the issue.

                _dgTemp.CommitEdit();
                object SelectedItem = _dgTemp.SelectedItem;
                DataGridRow _dgRow = DataGridHelper.GetRow(_dgTemp, _dgTemp.Items.IndexOf(SelectedItem));
                DataGridCell _dgCell = DataGridHelper.GetCell(_dgTemp, _dgRow, _dgTemp.Columns.IndexOf(_dgTemp.CurrentColumn));
                _dgCell.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
                _dgTemp.ScrollIntoView(_dgTemp.Items.IndexOf(_dgTemp.CurrentItem));
                _dgTemp.UpdateLayout();
                _dgTemp.SelectedIndex = _dgTemp.Items.IndexOf(_dgTemp.CurrentItem);

Upvotes: 0

Aran Mulholland
Aran Mulholland

Reputation: 23945

Try this little helper method for selecting a cell based on a column and a row item

private static void SelectCell(DataGrid dataGrid, DataGridColumn column, Object rowItem) {
         if (rowItem != null) {
            //scroll the item into view
            dataGrid.ScrollIntoView(rowItem);
            dataGrid.ScrollIntoView(rowItem, column);
            //get the cell info
            DataGridCellInfo cellInfo = new DataGridCellInfo(rowItem, column);

            if (dataGrid.CurrentCell.Item == cellInfo.Item && dataGrid.CurrentCell.Column == cellInfo.Column) { }
            else {
               dataGrid.Focus();
               dataGrid.CurrentCell = cellInfo;

               //set the cell to be selected
               dataGrid.SelectedCells.Clear();
               dataGrid.SelectedCells.Add(cellInfo);
            }
         }
      }

Upvotes: 1

Related Questions