How to Set the Color of a Selected Row in Datagrid

How can I set the color of a selected row in DataGrid

Got it. Add the following within the DataGrid.Resources section:

  <DataGrid.Resources>
<Style TargetType="{x:Type dg:DataGridCell}">
<Style.Triggers>
<Trigger Property="dg:DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="#CCDAFF" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>

DataGridView dynamic selected row Color

There is a separate property SelectionBackColor in DefaultCellStyle. Use this to change the selection color. You can have the default cell style stored and use this for restoring the default values.

Sample Code:

public class BetterDataGridView : DataGridView
{
private DataGridViewCellStyle defaultStyle = new DataGridViewCellStyle();
public BetterDataGridView()
{

}

protected override void OnRowStateChanged(int rowIndex, DataGridViewRowStateChangedEventArgs e)
{
base.OnRowStateChanged(rowIndex, e);
if (rowIndex > -1)
{
DataGridViewRow row = this.Rows[rowIndex];
if (row.Selected)
{
Color oldColor = this.CurrentRow.DefaultCellStyle.SelectionBackColor;
e.Row.DefaultCellStyle.SelectionBackColor = Color.FromArgb(oldColor.R < 235 ? oldColor.R + 20 : 0,
oldColor.G, oldColor.B);
}
else if (!row.Selected)
{
e.Row.DefaultCellStyle.SelectionBackColor = defaultStyle.SelectionBackColor;
}
}
}
}

WPF DataGrid selected row style

Use CellStyle and RowStyle on DataGrid. DataGridCell and DataGridRow both have IsSelected property that can be used in a Trigger to find out if they are selected.

Something like following should do the trick:

<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="White" />
<Setter Property="Foreground"
Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="BorderBrush"
Value="Blue" />
<Setter Property="BorderThickness"
Value="2" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>

Just play around until you get it right.

Data grid selected row color change (still don't get it)

Could this packages cause me problems ??

Yes, MahApps.Metro overrides the default DataGrid styles and uses different resource keys than the system style.

You simply need to adapt the answer from the linked question to accommodate the MahApps resource keys:

<DataGrid.Resources>
<SolidColorBrush x:Key="MetroDataGrid.HighlightBrush"
Color="Crimson" /> <!-- Set your own color here -->
</DataGrid.Resources>

There are some related background brushes you might want to override too:

  • MetroDataGrid.InactiveSelectionHighlightBrush
  • MetroDataGrid.MouseOverHighlightBrush
  • MetroDataGrid.DisabledHighlightBrush

And some foreground brushes:

  • MetroDataGrid.HighlightTextBrush
  • MetroDataGrid.InactiveSelectionHighlightTextBrush

You can inspect the MahApps styles on GitHub to see what brush resources they use, and when.



Related Topics



Leave a reply



Submit