Prevent CellForeColor and CellBackColor from being set to the same values (Conditional Formatting)

1 Answer 70 Views
DataLayout GridView Menu Styling
Richard
Top achievements
Rank 1
Richard asked on 28 Feb 2023, 09:35 PM

Hello!

I am currently using RadGridView for a project my company has me working on. We want to be able to prevent users from setting CellForeColor and CellBackColor to the same values (as seen in the image below). I've tried implementing a check on a variety of different elements to try and catch this case if it happens, but so far I've not had much luck. I guess more than anything I'm wondering which element I should be focusing on in order to catch this when it happens? I've scoured these forums and the rest of the internet and I haven't seen anything similar being asked. Any help is greatly appreciated, even if it's just a point in the right direction!

Best,

Rich

1 Answer, 1 is accepted

Sort by
1
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 01 Mar 2023, 09:18 AM

Hi Richard,

Thank you for the provided information regarding your requirement.

The Format section of the Conditional Formatting Rules Manager is represented by a RadPropertyGrid element. In your case, you need to access it and subscribe to its PropertyValueChanging event where you can get the desired properties and cancel the value change event if necessary. In order to access the conditional formatting form, you need to subscribe to the ConditionalFormattingFormShown event of the RadGridView. Inside the event handler, the first control in the Controls collection will be the RadPropertyGrid. Then you can subscribe to the PropertyValueChanging event of the RadPropertyGrid. In the second event handler, you can compare the two property values.

this.radGridView1.ConditionalFormattingFormShown += RadGridView1_ConditionalFormattingFormShown;

private void RadGridView1_ConditionalFormattingFormShown(object sender, EventArgs e)
{
    var conditionalFormattingForm = sender as ConditionalFormattingForm;
    var propertyGrid = conditionalFormattingForm.Controls[0] as RadPropertyGrid;
    propertyGrid.PropertyValueChanging -= PropertyGrid_PropertyValueChanging;
    propertyGrid.PropertyValueChanging += PropertyGrid_PropertyValueChanging;
}

private void PropertyGrid_PropertyValueChanging(object sender, PropertyGridItemValueChangingEventArgs e)
{
    var proertyGridTableElement = sender as PropertyGridTableElement;
    var propertyGridItem = e.Item;
    var propertyGridElement = e.PropertyGridElement;
    if (e.Item.Name == "CellForeColor" )
    {
        var newColor = e.NewValue;
        if(propertyGridElement.PropertyItems.FirstOrDefault(x=>x.Name == "CellBackColor").Value.ToString() == newColor.ToString())
        {
            e.Cancel = true;
            MessageBox.Show("CellForeColor and CellBackColor properties have the same colors!");
        }
    }
    else if(e.Item.Name == "CellBackColor")
    {
        var newColor = e.NewValue;
        if (propertyGridElement.PropertyItems.FirstOrDefault(x => x.Name == "CellForeColor").Value.ToString() == newColor.ToString())
        {
            e.Cancel = true;
            MessageBox.Show("CellForeColor and CellBackColor properties have the same colors!");
        }
    }
}

As a side note, by default, these two properties can set equal values when the user is choosing a color from the Color Dialog. However, if the user manually types the name of the color, it will be accepted. I think that you want to handle this scenario.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Richard
Top achievements
Rank 1
commented on 01 Mar 2023, 03:17 PM

Hi Dinko,

This looks exactly like what I was looking for, thank you so much! I'll let you know how this works out for me.

Best,

Rich

Richard
Top achievements
Rank 1
commented on 01 Mar 2023, 06:11 PM

Yep, that did just what I needed it to do. Thanks again Dinko!
Tags
DataLayout GridView Menu Styling
Asked by
Richard
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or