Ticket T554628
Visible to All Users

Change cell background color through code

created 8 years ago

I'm looking for a way to change the background color for a cell through code, or maybe a better way to accomplish my goal.

I have a grid with multiple columns. Some of the columns aren't used depending on the value of other columns. So for example, I have a grid to define some fields that will be added to a table in a database. The first column is to select the data type, which will be Text or Numeric. I then have a column for Text Length, where the user will enter the max length for text in the field. If the user picks Numeric as the type, the Text Length cell, for that row, needs to be locked so the user can't enter anything.

To do this in a generic way, I use the OnCustomColumnDisplayText event in my grid and added a IsValueRequired(string fieldName) method in my model. If the IsValueRequired method returns false, I set the display text to string.empty. I also use the OnShowingEditor event to disable showing the editor if IsValueRequired returns false.

This works perfectly and I can show a blank value where the value isn't required and the cell is disabled so the user can't click into.

To make it more obvious to the user that the cell won't allow input, I would like to change the cell background color to grey.

Is it possible to do this trough code with the grid events? If not, is it possible to do this with a style? If it's a style, I'm not sure what I would use as the trigger for how the cell is displayed.

In some other places we we ShowAsNullText in OnCustomColumnDisplayText. Is there a way to change the cell style base on whether ShowAsNullText is being used in the cell?

Thanks for any help you can provide. I can throw together a quick example app if needed.

Answers approved by DevExpress Support

created 8 years ago (modified 8 years ago)

Hi Marc,

You can use the CustomCellAppearance event to conditionally set the cell background in code.

C#
private void view_CustomCellAppearance(object sender, DevExpress.Xpf.Grid.CustomCellAppearanceEventArgs e) { if (e.Property == TextElement.BackgroundProperty && !((Item)grid.GetRow(e.RowHandle)).IsValueRequired(e.Column.FieldName)) { e.Result = Brushes.LightGray; e.Handled = true; } }

I've attached a sample project that demonstrates this approach in action.

Thanks,
Alex

    Show previous comments (9)
    Kirill (DevExpress Support) 8 years ago

      You are welcome!

        Hi Kirill,

        I am having problem setting cell color on cell value change. I am able to set the color in code but it is not reflecting on the cell right away.

        I am using below code :-

        private void view_CellValueChanged(object sender, DevExpress.Xpf.Grid.CellValueChangedEventArgs e)
        {
        var view = NominationGrid.View as TableView;
        if (view != null)
        {

        if (e.Column.FieldName.Split('.').Last() == "Status")
        {
        string cellValue = e.Value.ToString();
        if (cellValue == "Rejected")
        ((DataViewBase)sender).Background = Brushes.LightBlue;

        }

        view.CommitEditing();
        return;
        }
        }
        And used it in xaml like below : (Ignore PFWGridControl as we made a user control inherited from GridControl of DevExpress.Xpf.Grid)

        edx:PFWGridControl.View
        <dxg:TableView ShowGroupPanel="False" CellStyle="{StaticResource NominationGridCellStyle}" CellValueChanged="view_CellValueChanged">
        <!–dxg:TableView.FormatConditions
        <dxg:FormatCondition ValueRule="Equal" Value1="0" FieldName="RowData.Row.Status" PredefinedFormatName="LightRedFill"/>
        </dxg:TableView.FormatConditions>–>
        </dxg:TableView>
        </edx:PFWGridControl.View>

        I used NominationGridCellStyle as a resource which only work when screen get reopen after changed data which is fine.

        Thanks,
        Amit

        Dima (DevExpress Support) 5 years ago

          Hello Amit,

          Not to mix different questions in one thread, I created a separate ticket on your behalf - How to change a cell background color when its value changes. It has been placed in our processing queue and will be answered shortly.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.