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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:59:43+00:00 2026-05-17T23:59:43+00:00

I can’t seem to get my head around this. I’ve created sample code to

  • 0

I can’t seem to get my head around this.

I’ve created sample code to demo my issue, hoping someone can direct me to the answer…

The issue is once the datagrid is sorted, the labeled ID and Name no longer matches the selected datagrid item.

I would appriciate your assistance…

Thanks

Jeff

<Window x:Class="dgSortTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="dgSortTest" Height="253" Width="403" IsEnabled="True">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="212" HorizontalAlignment="Left" Margin="12,2,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" SelectionChanged="dataGrid1_SelectionChanged" RowHeaderWidth="0" AreRowDetailsFrozen="False" CanUserAddRows="True" CanUserDeleteRows="True" IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        </DataGrid.Columns>
        </DataGrid>
        <Label Content="Index: " Name="lblIndex" Height="28" HorizontalAlignment="Left" Margin="228,12,0,0" VerticalAlignment="Top" Width="92" />
        <Label Content="ID:" Name="lblID" Height="28" HorizontalAlignment="Left" Margin="228,46,0,0" VerticalAlignment="Top" Width="141" IsEnabled="True" />
        <Label Content="Name: " Name="lblName" Height="28" HorizontalAlignment="Left" Margin="228,80,0,0" VerticalAlignment="Top" Width="141" />
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace dgSortTest
{
    public partial class MainWindow : Window
    {
       List<Person> people = new List<Person>();
       public MainWindow()
        {
            InitializeComponent();
            people.Add(new Person() { ID = 0, Name = "Jeff" });
            people.Add(new Person() { ID = 1, Name = "Tom" });
            people.Add(new Person() { ID = 2, Name = "Andy" });
            people.Add(new Person() { ID = 3, Name = "Ken" });
            people.Add(new Person() { ID = 4, Name = "Zack" });
            people.Add(new Person() { ID = 5, Name = "Emily" });
            people.Add(new Person() { ID = 6, Name = "Courtney" });
            people.Add(new Person() { ID = 7, Name = "Adam" });
            people.Add(new Person() { ID = 8, Name = "Brenda" });
            people.Add(new Person() { ID = 9, Name = "Bill" });
            people.Add(new Person() { ID = 10, Name = "Joan" });
            dataGrid1.ItemsSource = from Person in people select Person;
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = dataGrid1.SelectedIndex;
            lblIndex.Content = "Index: " + index.ToString();
            lblID.Content = "ID: " + people[index].ID;
            lblName.Content = "Name: " + people[index].Name;
        } 
    }

    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}
  • 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-17T23:59:43+00:00Added an answer on May 17, 2026 at 11:59 pm

    Good description of your problem and short sample, +1 for that.

    Short answer, to not change to much of your current implementation you can do this.

    private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Person selectedItem = dataGrid1.SelectedItem as Person;
        int index = dataGrid1.SelectedIndex;
        lblIndex.Content = "Index: " + index.ToString();
        lblID.Content = "ID: " + selectedItem.ID;
        lblName.Content = "Name: " + selectedItem.Name;
    }
    

    However, I would advice you to bind directly to the SelectedItem instead. Then you won’t need the code behind EventHandler.

    <StackPanel Orientation="Vertical" Margin="228,12,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="92" >
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Index: "/>
            <TextBlock Text="{Binding ElementName=dataGrid1, Path=SelectedIndex}"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="ID: "/>
            <TextBlock Text="{Binding ElementName=dataGrid1, Path=SelectedItem.ID}"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Name: "/>
            <TextBlock Text="{Binding ElementName=dataGrid1, Path=SelectedItem.Name}"/>
        </StackPanel>
    </StackPanel>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can find why i get this error can someone help? package Android.data; public class
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object
Can someone help me with this scenario? *There is a button, which when tapped,
Can i get the source code for a WAMP stack installer somewhere? Any help
can someone tell me why sed won't remove my NULLs? this is on OS
Can someone please tell me why this wont work? I am getting error preg_match_all():
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can any one tell, how to get the result of LINQ query contains group

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.