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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:00:39+00:00 2026-05-29T07:00:39+00:00

I have a DataGrid a user can add items to by entering data in

  • 0

I have a DataGrid a user can add items to by entering data in the last row. I also have a button that deletes the currently selected item. But when the last (empty, for adding new items) row is selected, whatever was the last selected item remains in SelectedItem. So if I open the window, select the last row, and press the delete button, it will delete the first row, as it is selected by default, and selecting the last row did not change SelectedItem. Any good way to deal with this?

To clarify:
SelectedItem=”{Binding X}”

X in the ViewModel does not change when the last row is selected (the setter isn’t invoked at all). I’m not sure whether the SelectedItem property itself changes, but I would assume it doesn’t.

There is also an exception when I select the last row (red border), but when I click it again to start entering data, the red border disappers. Not sure if these two are related.

  • 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-29T07:00:40+00:00Added an answer on May 29, 2026 at 7:00 am

    Run the following example and you’ll see why it doesn’t work.

    XAML:

    <Window x:Class="DataGridTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <DockPanel>
            <TextBlock DockPanel.Dock="Bottom" Text="{Binding SelectedItem, ElementName=dataGrid}"/>
            <TextBlock DockPanel.Dock="Bottom" Text="{Binding SelectedItem}"/>
            <DataGrid x:Name="dataGrid" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" CanUserAddRows="True" CanUserDeleteRows="True" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
                    <DataGridTextColumn Header="Last Name" Binding="{Binding FirstName}"/>
                </DataGrid.Columns>
            </DataGrid>
        </DockPanel>
    </Window>
    

    Code-behind:

    namespace DataGridTest
    {
        using System.Collections.Generic;
        using System.Collections.ObjectModel;
        using System.ComponentModel;
        using System.Windows;
    
        public partial class MainWindow : Window, INotifyPropertyChanged
        {
            private readonly ICollection<Person> items;
            private Person selectedItem;
    
            public MainWindow()
            {
                InitializeComponent();
    
                this.items = new ObservableCollection<Person>();
                this.items.Add(new Person
                    {
                        FirstName = "Kent",
                        LastName = "Boogaart"
                    });
                this.items.Add(new Person
                {
                    FirstName = "Tempany",
                    LastName = "Boogaart"
                });
    
                this.DataContext = this;
            }
    
            public ICollection<Person> Items
            {
                get { return this.items; }
            }
    
            public Person SelectedItem
            {
                get { return this.selectedItem; }
                set
                {
                    this.selectedItem = value;
                    this.OnPropertyChanged("SelectedItem");
                }
            }
    
            private void OnPropertyChanged(string propertyName)
            {
                var handler = this.PropertyChanged;
    
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
        public class Person
        {
            public string FirstName
            {
                get;
                set;
            }
    
            public string LastName
            {
                get;
                set;
            }
    
            public override string ToString()
            {
                return FirstName + " " + LastName;
            }
        }
    }
    

    As you can see when running, selecting the "new" row causes a sentinel value to be set as the selected item in the DataGrid. However, WPF is unable to convert that sentinel item to a Person, so the SelectedItem binding fails to convert.

    To fix this, you could put a converter on your binding that detects the sentinel and returns null when detected. Here’s a converter that does so:

    namespace DataGridTest
    {
        using System;
        using System.Windows.Data;
    
        public sealed class SentinelConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return value;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value.Equals(CollectionView.NewItemPlaceholder))
                {
                    return null;
                }
                return value;
            }
        }
    }
    

    As you can see, it is an unfortunate necessity to test against the ToString() value of the sentinel, because it is an internal type. You could alternatively (or in addition) check that GetType().Name is NamedObject.

    • 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 data. You can add rows through a
I have a datagrid which is populated with CSV data when the user drag/drops
I have a program in which a user selects a row in a Datagrid
I have a datagrid that I want the user to sort the rows on.
Using .NET 1.1, I have a DataGrid that contains three columns for each row.
i have a datagrid and every row has a checkbox on it. also every
I have a datagrid, populated as shown below. When the user clicks on a
I have a DataGrid, with an ItemTemplate that has an image and label. In
I have a DataGrid with 5 template columns, However when I try and add
I have a dataGrid(not dataGridView) in which i need to add Checkbox dynamically at

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.