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 6619721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:00:23+00:00 2026-05-25T21:00:23+00:00

I have WPF DataGrid placed on the Window . I have a button which

  • 0

I have WPF DataGrid placed on the Window. I have a button which performs some business logic and populates the grid.

I want to set focus on the DataGrid (preferably first row in the DataGrid) when i TAB from the button. I have set the TabIndex but somehow the focus does not come on the DataGrid.

The relevant part of the form’s XAML is here:

<StackPanel Orientation="Vertical" Grid.Column="2" Margin="20,10,10,10">
    <Button Content="Search"
                Height="25" HorizontalAlignment="Right" Margin="0,-25,0,0"
                Name="btnSearch" VerticalAlignment="Top" Width="75" **TabIndex="1300"** Click="btnSearch_Click" Keyboard.KeyDown="btnSearch_PreviewKeyDown" LostFocus="btnSearch_LostFocus"/>
</StackPanel>
<DataGrid AutoGenerateColumns="False" Grid.Column="1" Margin="20,160,10,10" Name="dataGridOrganisations" **TabIndex="1400"**
          BorderThickness="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Focusable="True"
                ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" GridLinesVisibility="None"
           ItemsSource="{Binding}" CanUserReorderColumns="False" CanUserResizeRows="False" IsReadOnly="True" SelectionChanged="dataGridOrganisations_SelectionChanged" Keyboard.PreviewKeyDown="dataGridOrganisations_PreviewKeyDown"  >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Shortname" Width="100" Binding="{Binding ShortName}" />
        <DataGridTextColumn Header="Internal Code" Width="100" Binding="{Binding LocalID}"/>
        <DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}"/>
    </DataGrid.Columns>
</DataGrid>

When I added following code on Button’s LostFocus event, it highlights the first row. But when I use ‘down’ arrow key to select next row in the grid; instead of going to next row it first sets focus on the ‘Search’ button and next time goes to second row!

if (this.dataGridOrganisations != null && this.dataGridOrganisations.HasItems)
{
    this.dataGridOrganisations.Focus();
    if (dataGridOrganisations.Items != null && dataGridOrganisations.Items.Count > 0)
    {
        DataGridRow firstRow = this.dataGridOrganisations.ItemContainerGenerator.ContainerFromItem(dataGridOrganisations.Items[0]) as DataGridRow;
        if (firstRow != null)
        {
            firstRow.IsSelected = true;
        }
    }
}

How to set focus on the DataGrid (or its first row)? Why TabIndex does not work here?

  • 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-25T21:00:24+00:00Added an answer on May 25, 2026 at 9:00 pm

    You do not even need a tab-index if there are no focusable controls between the Button and DataGrid in the control hierarchy. You have quite a few handlers and everything seems a bit convoluted to me. I cannot spot anything in that code (maybe someone else can of course), my suggestion would be that you try to simplify your code until it works again as it should by default. e.g. this code’s tabbing should work:

      <StackPanel>
        <Button Content="Lorem Ipsum"/>
        <DataGrid ItemsSource="{Binding Source={StaticResource Items}}">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}"/>
            </DataGrid.Columns>
        </DataGrid>
      </StackPanel>
    

    Also, why the large tab-index delta? If you were to use 1301 & 1302 there could not be any value between so it should automatically be safer code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF DataGrid with some styling, properties and events. I want to
I have wpf datagrid with number of template columns. some of them have textbox
I have a WPF datagrid and it works great but I notice some sort
I have a DataGrid in WPF. And I am trying to add Button s
I have a WPF DataGrid that I want to be able to Update, Insert
I have created a WPF tooklit datagrid in C# and the ItemsSource is set
I have a DataGrid, and in that grid, some of the columns are marked
I have a WPF DataGrid bound to some entities (Entity Framework 4+). User then
I have a WPF Toolkit DataGrid control, contained within a Grid, inside a ViewBox
I have a performance issue with the WPF DataGrid (.net 4.0) first, some details:

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.