Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7583899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:47:39+00:00 2026-05-30T18:47:39+00:00

When the DataGrid below gets the focus for the first time and only the

  • 0

When the DataGrid below gets the focus for the first time and only the first time (ie, after some other control has had the focus), the last row, 2nd column should be focused and in edit.

enter image description here

I added a handler for the DataGrid.GotFocus, but it’s complicated code and not getting the result above.

Anyone got an elegant, bullet proof solution?


I made tiny modifications to the code

  1. the sender should always be the grid I want, so I just used that instead of relying on a name
  2. When the SelectionUnit is FullRow, as my grid was before I changed it to CellOrRowHeader you
    apparently can’t call SelectedCells.Clear()

Code below:

private void OnDataGridKeyboardGotFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    var dg = sender as DataGrid;
    if (_hasHadInitialFocus) return;

    var rowIndex = dg.Items.Count - 2;
    if (rowIndex >= 0 && dg.Columns.Count - 1 >= 0)
    {
        var column = dg.Columns[dg.Columns.Count - 1];
        var item = dg.Items[rowIndex];
        var dataGridCellInfo = new DataGridCellInfo(item, column);

        if (dg.SelectionUnit != DataGridSelectionUnit.FullRow) {
            dg.SelectedCells.Clear();
            dg.SelectedCells.Add(dataGridCellInfo);
        }
        else {
            var row = dg.GetRow(rowIndex);
            row.IsSelected = true;
        }

        dg.CurrentCell = dataGridCellInfo;
        dg.BeginEdit();
    }

    _hasHadInitialFocus = true;
}

New Question

I want to repeat the selection when the focus goes to another control in the window and then back to the grid.
I thought I could turn that _hasHadInitialFocus latch to false in a LostFocus event, but the code below is firing on cell changes.
Do you know how I should be trapping the lost focus event better, and do you agree that is the place to turn the latch off?

    private void DataGridLostFocus(object sender, RoutedEventArgs e) {
        _hasHadInitialFocus = false;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:47:40+00:00Added an answer on May 30, 2026 at 6:47 pm

    You may have to fiddle with the offsets depending on whether there’s an new item row visible or not, but this works for me.

        private bool _hasHadInitialFocus;
    
        private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (!_hasHadInitialFocus)
            {
                if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
                {
                    var dataGridCellInfo = new DataGridCellInfo(
                        dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);
    
                    dataGrid.SelectedCells.Clear();
                    dataGrid.SelectedCells.Add(dataGridCellInfo);
                    dataGrid.CurrentCell = dataGridCellInfo;
                    dataGrid.BeginEdit();
                }
    
                _hasHadInitialFocus = true;
            }
        }
    

    I noticed that clicking into the grid leaves one cell selected and the target cell in edit mode. A solution to this if required is:

        private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            EditCell();
        }
    
        private void PreviewMouseLBDown(object sender, MouseButtonEventArgs e)
        {
            if (!_hasHadInitialFocus)
            {
                e.Handled = true;
                EditCell();
            }
        }
    
        private void EditCell()
        {
            if (!_hasHadInitialFocus)
            {
                if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
                {
                    var dataGridCellInfo = new DataGridCellInfo(
                        dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);
    
                    dataGrid.SelectedCells.Clear();
                    dataGrid.SelectedCells.Add(dataGridCellInfo);
                    dataGrid.CurrentCell = dataGridCellInfo;
                    dataGrid.BeginEdit();
                }
    
                _hasHadInitialFocus = true;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page (below) that has a datagrid that lists item's returned from
my application needs to allow users to insert rows below the current datagrid row.
I've got a datagrid with one button in each row (xaml shown below). I'm
I need to develop Advanced Datagrid like the below attached image. So I had
I'm populating a datagrid using Linq--standard kind of stuff (code below). For some reason
I have a main datagrid, then an accordion control below it. In one of
I've retemplated the DataGridRow in the Microsoft WPF DataGrid to the below, the problem
I have a datagrid, populated as shown below. When the user clicks on a
I have a DataGrid where each column has a SortExpression. I would like the
I have a DataGrid, populated with objects in an ArrayCollection. After updating one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.