I have a simple WPF application that shows about 30-35,000 rows of data in a datagrid and if a user changes any cell value, that cell should be highlighted by changing its background color. Columns are generated at runtime depending on the data. Given the quantity of the data it holds, virtualized datagrid is a must. Here is my datagrid:
<DataGrid x:Name="dgPropertyData" HeadersVisibility="Column" ColumnHeaderHeight="25"
BorderThickness="0" AutoGenerateColumns="false"
HorizontalGridLinesBrush="#FFC7C5C5"
VerticalGridLinesBrush="#FFC7C5C5"
MinRowHeight="20" CanUserResizeRows="False"
CanUserDeleteRows="False" CanUserReorderColumns="False"
EnableRowVirtualization="true"
AutoGeneratedColumns="dgPropertyData_AutoGeneratedColumns"
CellEditEnding="dgPropertyData_CellEditEnding">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
I am changing a cell background color inside a code-behind method:
private void dgPropertyData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
string oldValue = (e.Row.Item as BindableDynamicDictionary)[e.Column.Header as string] as string;
string newValue = (e.EditingElement as System.Windows.Controls.TextBox).Text;
if ((oldValue != null && oldValue.CompareTo(newValue) != 0) || (oldValue == null && string.IsNullOrEmpty(newValue) == false))
{
(e.Column.GetCellContent(e.Row).Parent as DataGridCell).Background = Brushes.Orange;
IsCellEdited = true;
}
}
My Issue: When I edit a cell the background changes nicely to notify that the value has been changed. Only until.... I scroll the grid view. On scrolling, edited cells get a wipe and wrong cells get painted (if at all).
I have gone through following questions to get some ideas:
- Changing color of a single cell in WPF Datagrid
- Wpf Datagrid Virtualization Issue when setting cell colors
- RowVirtualization cause incorrect background color for rows
But haven't quite found a solution for my purpose. Being very new to WPF world, I get a mind block quite often as how to solve this problem. I understand that the issue of losing the cell background color is because of recycling of the rows and that if I disable the virtualization, the performance hit is unbearable for the amount of data I have.