This is a migrated thread and some comments may be shown as answers.

Performing a Custom Business Action When a Condition is Met

4 Answers 32 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 04 Dec 2013, 05:10 PM
I'm using a gridview in my C# Winforms project and I need to both perform conditional formatting and fire certain custom actions (such as emailing someone, playing a sound, popping a Desktop Alert, etc) when a condition is met.  I've set up a conditional formatting framework which works great...when a condition is met it formats the cell or row differently (for instance, if an Order Totals cell value falls below 20 in a row then it will turn the text red).  My problem is trying to perform these custom, non-formatting actions when this condition is met.  The only way I've made this work is to use the CellFormatting event and check each cell for the condition(s) after each cell is bound, then perform the action at that time.  I would much rather just create a ConditionalFormattingObject (or something similar), apply that to a column, and perform all actions and formatting changes at the same time.  Is this possible?

Not sure if it matters, but the datagrid is being bound to a List<> of custom business objects returned from a WCF service.

4 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 09 Dec 2013, 01:37 PM
Hello Scott,

Thank you for contacting Telerik Support.

ConditionalFormattingObjects are purposed to be used for customizing cells according to a certain condition. Similar objects for performing a certain set of actions, when a condition is met, is not available. However, I can suggest to create a custom ConditionalFormattingObject and override its Evaluate method, where if the evaluation is successful, you are allowed to call the desired methods for processing the necessary actions:
private void Form1_Load(object sender, EventArgs e)
{
    this.productsTableAdapter.Fill(this.nwindDataSet.Products);
 
    CustomConditionalFormattingObject obj = new CustomConditionalFormattingObject("MyCondition", ConditionTypes.Greater, "30", "", false);
    obj.CellBackColor = Color.SkyBlue;
    obj.CellForeColor = Color.Red;
    obj.TextAlignment = ContentAlignment.MiddleRight;
    this.radGridView1.Columns["UnitPrice"].ConditionalFormattingObjectList.Add(obj);
     
}
 
public class CustomConditionalFormattingObject : ConditionalFormattingObject
{
    public CustomConditionalFormattingObject(string name, ConditionTypes type, string tvalue1, string tvalue2, bool applyToRow)
        :base( name,  type,  tvalue1,  tvalue2,  applyToRow)
    {
    }
 
    public override bool Evaluate(GridViewRowInfo row, GridViewColumn column)
    {
        bool conditionMet = base.Evaluate(row, column);
        if (conditionMet)
        {
            Console.WriteLine("Condition met for row:{0},col{1}",row.Index,column.Index);
        }
        
        return conditionMet;
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Scott
Top achievements
Rank 1
answered on 20 Dec 2013, 07:07 PM
This worked perfectly, thank you!
0
Scott
Top achievements
Rank 1
answered on 27 Dec 2013, 03:20 PM
I spoke too soon.  We're on the right track here, but I found an issue with the method you proposed.  It appears the Evaluate method doesn't get called for every row in the gridview.  I set a breakpoint in the Evaluate method and its only hit about 20 times, even though there are 120 rows being bound to the grid.  I noticed when I scroll through the grid the Evaluate method is fired again, so I'm guessing there is some kind of dynamic evaluation that occurs as you scroll through the grid to avoid having to evaluate the entire datasource at once.  The problem is, I need to evaluate everything at once.  I need these custom business actions to occur even if the row hasn't been painted onscreen yet.  Is this possible?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Dec 2013, 03:36 PM
Hello Scott,

Thank you for contacting Telerik Support.

Due to the UI virtualization in RadGridView, cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, grouping and so on. That is why ConditionalFormattingObject.Evaluate method is fired for the newly visible cells when scrolling. Note that ConditionalFormattingObject is purposed to apply a certain style for a visible cell only. If your requirement is to evaluate all cell and to perform a custom actions on all of them, the appropriate solution that I can suggest is to iterate through all the cells and check if the desired condition is met. Then perform the necessary actions.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Scott
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Scott
Top achievements
Rank 1
Share this question
or