Click here to Skip to main content
15,042,336 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
Hello..

I am using visual studio 2019, and doing a project WPF using Telerik UI.
I trying to change cell color of gridview with conditons..

please help me...

What I have tried:

radGridView1.ItemsSource = getdetails_ds.DefaultView;
this.radGridView1.Rows[0].Cells[1].Style.ForeColor = System.Drawing.Color.Red;
Posted
Updated 12-Sep-20 4:00am

1 solution

Use built-in conditional formatting functionality:
WPF DataGrid | CellStyleSelector | Telerik UI for WPF[^]
WPF DataGrid | Styling Cells | Telerik UI for WPF[^]
Example:
C#
public class StadiumCapacityStyle : StyleSelector 
{ 
    public override Style SelectStyle(object item, DependencyObject container) 
    { 
        if (item is Club) 
        { 
            Club club = item as Club; 
            if (club.StadiumCapacity > 50000) 
            { 
                return BigStadiumStyle; 
            } 
            else 
            { 
                return SmallStadiumStyle; 
            } 
        } 
        return null; 
    } 
    public Style BigStadiumStyle { get; set; } 
    public Style SmallStadiumStyle { get; set; } 
}

XAML
HTML
<Grid.Resources> 
    <my:StadiumCapacityStyle x:Key="stadiumCapacityStyle"> 
        <my:StadiumCapacityStyle.BigStadiumStyle> 
            <Style TargetType="telerik:GridViewCell"> 
                <Setter Property="Background" Value="Red"/> 
            </Style> 
        </my:StadiumCapacityStyle.BigStadiumStyle> 
        <my:StadiumCapacityStyle.SmallStadiumStyle> 
            <Style TargetType="telerik:GridViewCell"> 
                <Setter Property="Background" Value="Yellow" /> 
            </Style> 
        </my:StadiumCapacityStyle.SmallStadiumStyle> 
    </my:StadiumCapacityStyle> 
</Grid.Resources>
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900