Articles in this section
Category / Section

How to change the Foreground of Selected Row in the GridDataControl based on the ConditionalFormats?

2 mins read

When you set the GridDataControl.Foreground, it applies foreground to GridDataControl and the selected row is displayed based on the GridDataControl.StyleManager.RowAppearence.HighlightSelectionForeground

When you want to set the same foreground to the non-selected rows and selected rows, you need to set the same foreground to the GridDataControl.Foreground and GridDataControl.StyleManager.RowAppearence.HighlightSelectionForeground properties as shown in the following code example.

<syncfusion:GridDataControl Name="grid "
                            ItemsSource="{Binding Employee}"
                            AutoPopulateColumns="False"
                            AllowSelection="Row"
                            ShowCurrentCell="False"
                            ShowHoveringBackground="True"
        Background="Transparent"
                            Foreground="Green"
</syncfusion:GridDataControl>
<syncfusion:GridDataControl.StyleManager>
    <syncfusion:GridDataStyleManager>
        <syncfusion:GridDataStyleManager.RowAppearence>
            <syncfusion:RowAppearence HighlightSelectionForeground="Green "/>
        </syncfusion:GridDataStyleManager.RowAppearence>
    </syncfusion:GridDataStyleManager>
</syncfusion:GridDataControl.StyleManager>

When setting foreground to the specific cell based on the ConditionalFormats, the selected row foreground is based on the GridDataControl.StyleManager.RowAppearence.HighlightSelectionForeground instead of ConditionalFormats Foreground. You can overcome this problem by setting the e.Style.Foreground within PrepareRenderCell event conditionally.

The following code example shows how to set the foreground to the specific cell based on the ConditionalFormats.

<syncfusion:GridDataControl.ConditionalFormats>
    <syncfusion:GridDataConditionalFormat Name="EmployeeID" ApplyStyleToColumn="EmployeeID">
        <syncfusion:GridDataConditionalFormat.Style>
            <syncfusion:GridDataStyleInfo Foreground="Blue" SelectionBrush="Blue"/>
        </syncfusion:GridDataConditionalFormat.Style>
        <syncfusion:GridDataConditionalFormat.Conditions>
            <syncfusion:GridDataCondition ColumnName="EmployeeID"
                                ConditionType="Equals"
                                Value="1000" />
        </syncfusion:GridDataConditionalFormat.Conditions>
    </syncfusion:GridDataConditionalFormat>
</syncfusion:GridDataControl.ConditionalFormats>

 

The following code example shows how to set the e.Style.Foreground within the PrepareRenderCell event based on the condition.

using Syncfusion.Windows.Controls.Grid;
public MainWindow()
{
    InitializeComponent();
    grid.ModelLoaded+=grid_ModelLoaded;
}
private void grid_ModelLoaded(object sender, EventArgs e)
{
    grid.Model.Grid.PrepareRenderCell += Grid_PrepareRenderCell;
}
void Grid_PrepareRenderCell(object sender, GridPrepareRenderCellEventArgs e)
{
    if (e.Style.RowIndex > 0)
    {
        var columnindex = this.grid.Model.ResolvePositionToVisibleColumnIndex(e.Style.ColumnIndex);
        if (columnindex >= 0 && this.grid.VisibleColumns[columnindex].MappingName == "EmployeeID")
        {
            var cell = this.grid.Model[e.Cell.RowIndex, e.Style.ColumnIndex].CellValue;
            int result = Convert.ToInt32(cell);
            if (result == 1000)
            {
                //set Selected row Foreground color
                e.Style.Foreground = Brushes.Blue;
            }
        }
        else
        {
            e.Style.Foreground = Brushes.Green;
        }
    }
}

 

Note:

By using the same way, you can set the Foreground to HoveringRecordCell with the help of the GridDataControl.StyleManager.RowAppearence.HoveringRecordCellForeground property in GridDataControl.

 

The following screenshot shows the output of the GridDataControl where the Foreground of Selected row is changed from its default color.

GridDataControl

 

You can refer to the following sample links to change the foreground color for selected row in GridDataControl based on Conditional formats.

Sample link: ForegroundColorForSelectedRow_WPF

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied