---
title: "preventing row change in datagrid"  
description: "preventing row change in datagrid"  
author: "Manoj Bhatt"  
published: 2013-09-24  
updated: 2013-09-24  
canonical: https://www.mindstick.com/forum/1512/preventing-row-change-in-datagrid  
category: "c#"  
tags: ["c#", "preventing row", "preventing row change in datagrid", "c# datagrid", "mindstick"]  
reading_time: 2 minutes  

---

# preventing row change in datagrid

I have researched this and am stumped: I have a WPF DataGrid, and using an MVVM model. I want to, under certain circumstances, prevent the ability to change a row in the DataGrid. I have researched this and have tried [techniques](https://www.mindstick.com/articles/13015/5-practical-tips-and-techniques-to-write-an-essay) like the one found here.

In [practice](https://yourviews.mindstick.com/story/1498/terrific-benefits-of-a-morning-meditation-practice), this works, however there is an undesirable 'flicker' (it selects the clicked row for a moment then goes back to the [previous](https://yourviews.mindstick.com/view/81421/unlock-2-0-is-just-like-previous-corona-lockdowns) [selection](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box)), while this is a close solution I wish there was a more elegant way such as [preventing](https://www.mindstick.com/news/2244/issue-preventing-users-from-accessing-facebook-s-social-networking-platforms-has-been-resolved) the row change at all in the first place.

I am surprised there is not a SelectionChanging or BeforeSelectionChanged so I could [cancel](https://answers.mindstick.com/qa/94501/how-can-i-cancel-my-singapore-flight-ticket) the [event](https://www.mindstick.com/articles/167719/marquee-hire-benefits-of-event-lighting-hire-london) from firing; and forcibly preventing the [index](https://www.mindstick.com/blog/198/index-in-sql-server) change in my [view model](https://www.mindstick.com/forum/160237/explain-razor-view-model-binding-how-does-it-work-and-why-is-it-important) does not seem to make any difference.

How can I do this?

Thank you.

## Replies

### Reply by Pravesh Singh

Hi Manoj,

Do you have a CellStyle on your datagrid? For me, the following worked:

## xaml:

```
<DataGrid.CellStyle>    <Style TargetType="{x:Type DataGridCell}">        <Style.Triggers>            <Trigger Property="IsSelected" Value="True">                <Setter Property="Background" Value="DarkSlateBlue"/>                <Setter Property="Foreground" Value="White"/>            </Trigger>        </Style.Triggers>    </Style></DataGrid.CellStyle>
```

## codebehind:

```
private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e){    if (e.AddedItems.Count > 0)    {        object x = e.AddedItems[0];        if (x is MyObjectType && x != myViewModel.CurrentItem &&            myViewModel.ShouldNotDeselectCurrentItem())        {            // this will actually revert the SelectedItem correctly, but it won't highlight the correct (old) row.            this.MyDataGrid.SelectedItem = null;            this.MyDataGrid.SelectedItem = myViewModel.CurrentItem;        }    }}
```

The point was that the SelectedCellsChanged event fired after the SelectionChanged event -- and in particular, that setting the SelectedItem does not correctly update the SelectedCells which are a read-only property, so more codebehind:

```
private void MyDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e){    List<DataGridCellInfo> selectedCells = MyDataGrid.SelectedCells.ToList();    List<MyObjectType> wrongObjects = selectedCells.Select(cellInfo => cellInfo.Item as MyObjectType)        .Where (myObject => myObject != myViewModel.CurrentItem).Distinct().ToList();    if (wrongObjects.Count > 0)    {        MyDataGrid.UnselectAllCells();        MyDataGrid.SelectedItem = null;        MyDataGrid.SelectedItem = myViewModel.CurrentItem;    }}
```


---

Original Source: https://www.mindstick.com/forum/1512/preventing-row-change-in-datagrid

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
