Programmatically assigning a color to a row in DataGrid

人盡茶涼 提交于 2019-12-20 11:32:24

问题


I need to assign a color to the row I add at runtime to the DataTable. How can this be done?


回答1:


You can handle the DataGrid's LoadingRow event to detect when a row is being added. In the event handler you can get a reference to the DataRow that was added to the DataTable that is acting as your ItemsSource. Then you can update the DataGridRow's color however you like.

void dataGrid_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e)
{
    // Get the DataRow corresponding to the DataGridRow that is loading.
    DataRowView item = e.Row.Item as DataRowView;
    if (item != null)
    {
        DataRow row = item.Row;

            // Access cell values values if needed...
            // var colValue = row["ColumnName1]";
            // var colValue2 = row["ColumName2]";

        // Set the background color of the DataGrid row based on whatever data you like from 
        // the row.
        e.Row.Background = new SolidColorBrush(Colors.BlanchedAlmond);
    }           
}

To sign up for the event in XAML:

<toolkit:DataGrid x:Name="dataGrid"
    ...
    LoadingRow="dataGrid_LoadingRow">

Or in C#:

this.dataGrid.LoadingRow += new EventHandler<Microsoft.Windows.Controls.DataGridRowEventArgs>(dataGrid_LoadingRow);



回答2:


U can try this

In the XAML

<Window.Resources>
<Style TargetType="{x:Type DataGridRow}">
    <Style.Setters>
        <Setter Property="Background" Value="{Binding Path=StatusColor}"></Setter>
    </Style.Setters>            
</Style>
</Window.Resources>

In the datagrid

<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" Name="dtgTestColor" ItemsSource="{Binding}" >
<DataGrid.Columns>                            
    <DataGridTextColumn Header="Valor" Binding="{Binding Path=Valor}"/>
</DataGrid.Columns>
</DataGrid>

In the code i have a class with

public class ColorRenglon
{
    public string Valor { get; set; }
    public string StatusColor { get; set; }
}

When set the DataContext

dtgTestColor.DataContext = ColorRenglon;
dtgTestColor.Items.Refresh();

If u not set the color of the row the default value is Grey

u can try this sample with this sample

List<ColorRenglon> test = new List<ColorRenglon>();
ColorRenglon cambiandoColor = new ColorRenglon();
cambiandoColor.Valor = "Aqui va un color"; 
cambiandoColor.StatusColor = "Red";
test.Add(cambiandoColor);
cambiandoColor = new ColorRenglon();
cambiandoColor.Valor = "Aqui va otro color"; 
cambiandoColor.StatusColor = "PaleGreen";
test.Add(cambiandoColor);



回答3:


IMPORTANT : be sure to always assign defaults for rows that aren't being colored by a condition - or any other style.

See my answer to C# Silverlight Datagrid - Row Color Change.

PS. I am in Silverlight and haven't confirmed this behavior in WPF



来源:https://stackoverflow.com/questions/1847359/programmatically-assigning-a-color-to-a-row-in-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!