Change DataGrid Color

1

Galera,    I need some help, I already researched but did not find,    I would like to know how to change the color of the selected item in the DataGrid.    Example: When I have a row in the datagrid and click on it it is selected with the blue color, I would like to change that color.    Can anyone give me a hint.

    
asked by anonymous 27.06.2018 / 16:05

2 answers

2

One possibility is to have a property on the object that is listing to do this:

public virtual int BackgroundId { get; set; }

In the click event you can change the value of this variable to differentiate it from the others:

seuObjeto.BackgroundId = 1;

In XAML, within the datagrid a datatrigger is created:

<DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding BackgroundId}" Value="1">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding BackgroundId}" Value="2">
                        <Setter Property="Background" Value="Yellow"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
</DataGrid.CellStyle>

From now on items that have this property changed will also have their background changed. In the example for 1 or 2, but you can set as many as you want in the datatrigger

    
15.08.2018 / 12:40
-1

Try this:

DataGridViewRow.DefaultCellStyle.ForeColor = Color.Red;
    
05.07.2018 / 00:27