WPF DataGrid- Foreground and Background on selection

Posted by: rambabu on 17 January 2024, 4:18 am EST

    • Post Options:
    • Link

    Posted 17 January 2024, 4:18 am EST

    Hello,

    I am working on Datagrid which contains the foreground color for each row dynamically driven from the data. I want to set the Selected Row text foreground color to Background and change the foreground color to a different color to view the selected row.

    Attached the video to show an example of how I want to implement in Datagrid, can you please provide a solution to it.

    https://www.awesomescreenshot.com/video/24125313?key=2843e1888b3d2a7f9ace18ed0f4166a0

  • Posted 17 January 2024, 7:44 pm EST

    Hi Rambabu,

    You can achieve the desired behavior by handling C1DataGrid’s LoadedCellPresenter event as shown in the following code snippet:

    datagrid.LoadedCellPresenter += (sender,e) =>
    {
        C1DataGrid dg = (C1DataGrid)sender;
        Product prod = (Product)e.Cell.Row.DataItem;
        Brush forecolor = null;
        double price = prod.Price;
        if(price > 500)
        {
            forecolor = e.Cell.Presenter.Foreground = Brushes.Green;                
        }
        else
        {
            forecolor = e.Cell.Presenter.Foreground = Brushes.Red;
        }
        if (dg.SelectedItem == e.Cell.Row.DataItem)
        {
            datagrid.SelectedBackground = forecolor;
            e.Cell.Presenter.Foreground = Brushes.Blue;
        }
    }

    Kindly refer to the attached sample for implementation. See DatagridColorUpdate.zip

    Please note that C1DataGrid is a legacy control and is out of support now. We recommend you migrate to C1FlexGrid in order to get latest support and features.

    Documentation link to WPF FlexGrid: https://developer.mescius.com/componentone/docs/wpf/online-flexgrid/overview.html

    Thanks & Regards,

    Aastha

  • Posted 25 January 2024, 4:58 am EST

    Thank you for the solution.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels