How to change the color of a Datagrid line in C#?

Asked

Viewed 1,314 times

1

When a Datagrid row was selected and then a button was clicked, the color of the selected row changed, I use Visual Studio 2013 and it is Wpf application.

2 answers

3

Conditional styling with WPF

You can also do validations in WPF, using datatriggers.
Note that this code snippet is located on Application.xaml, within the tag Application.Resources

 <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Tipo}" Value="Erro">
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                <GradientStop Color="White" Offset="0.2"/>
                                <GradientStop Color="#ffb1b1" Offset="0.85"/>
                                <GradientStop Color="WhiteSmoke" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="FontWeight" Value="Bold"/>
                </DataTrigger>

                <DataTrigger Binding="{Binding Path=Tipo}" Value="Aviso">
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                <GradientStop Color="White" Offset="0.2"/>
                                <GradientStop Color="#ffee86" Offset="0.85"/>
                                <GradientStop Color="WhiteSmoke" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="FontWeight" Value="Bold"/>
                </DataTrigger>

                <DataTrigger Binding="{Binding Path=Tipo}" Value="Info">
                    <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <GradientStop Color="White" Offset="0.2"/>
                            <GradientStop Color="#c6deff" Offset="0.85"/>
                            <GradientStop Color="WhiteSmoke" Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                    <Setter Property="FontWeight" Value="Bold"/>
                </DataTrigger>

                <DataTrigger Binding="{Binding Path=Tipo}" Value="Sucesso">
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                <GradientStop Color="White" Offset="0.2"/>
                                <GradientStop Color="#c6ffc7" Offset="0.85"/>
                                <GradientStop Color="WhiteSmoke" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="FontWeight" Value="Bold"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

In this example, the filters applied went to the column Type. The tag value is assigned to for example: if the row has in the column type the value Error, then the line will be in the color you assign.
If you don’t master WPF, you can create other setters beyond the one I created in the example, which applies to the row, as an example:

<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="1"/>
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="11px"/>
<Setter Property="FontWeight" Value="Normal"/>

For more information about this Styles/Templates, click here

2


This code snippet changes the color of the Row selected from Datagrid myDataGrid to red.

int selectedIndex = myDataGrid.SelectedIndex;
//Guarda a Row selecionada
DataGridRow row = 
          myDataGrid.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as DataGridRow;
if(row == null)//A linha selecionada não está visivel
{
   object item = myDataGrid.Items[selectedIndex];
   myDataGrid.ScrollIntoView(item);//Torna a linha selecionada visivel
   row = myDataGrid.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as DataGridRow;
}
//Altera a cor para vermelho
row.Background = Brushes.Red;

Note:
The estate Selectionunit of myDataGrid has to look like Fullrow

Browser other questions tagged

You are not signed in. Login or sign up in order to post.