We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

dynamic formula using gridGroupingContro

Thread ID:

Created:

Updated:

Platform:

Replies:

127361 Nov 10,2016 02:14 AM Feb 6,2017 12:00 AM Windows Forms 5
loading
Tags: GridGroupingControl
Gregory Pe
Asked On November 10, 2016 02:14 AM

 how to create a dynamic formula using gridGroupingControl.  as the link https://www.youtube.com/watch?v=Z0YCFjNxbXM


Amal Raj Umapathy Selvam [Syncfusion]
Replied On November 11, 2016 09:02 AM

Hi Gregory,
Thanks for using Syncfusion products. 

The reported scenario with the GridGroupingControl to create a dynamic formula can be achieved by using GridFormulaEngine. We have prepared a simple sample to create a dynamic formula with GridGroupingControl like the scenario from the provided video link. Please make use of the below code, 

Code Snippet 
private void button1_Click(object sender, EventArgs e) 
{ 
    GridFormulaEngine formulaEngine = new GridFormulaEngine(this.gridGroupingControl1.TableModel); 
 
    foreach (GridRecord rec in this.gridGroupingControl1.Table.Records) 
    { 
        double aValue = (double)rec["A"]; 
        int bValue = (int)rec["B"]; 
        double cValue = (double)rec["C"]; 
        if (rec["Formula"] != null) 
        { 
            string formula = rec["Formula"].ToString(); 
            //Replacing the given expesson with values. 
            if (formula.Contains('a')) 
                formula = formula.Replace("a", aValue.ToString()); 
            if (formula.Contains('b')) 
                formula = formula.Replace("b", bValue.ToString()); 
            if (formula.Contains("c")) 
                formula = formula.Replace("c", cValue.ToString()); 
            string result = formulaEngine.ComputeFormulaText(formula); 
            rec.SetValue("Result", result); 
        } 
    } 
} 
 
Sample Link 
 
Please refer to the video of our sample execution, 
 
Note 
We have created a dynamic formula based on the given expression like your requirement. You can create dynamic formula as per your own expression. 
 
Regards, 
Amal Raj U. 


Arda Beyazoglu
Replied On February 2, 2017 02:25 PM

Hello,

Is there a way to achieve something similar in DataBoundGrid control ? I tried but couldnt succeed.

Basically I have 4 columns and i made 4th column a formula cell. I want to make a calculation on first 3 columns and print result in 4th column. And this is same for each row in the grid.

Piruthiviraj Malaimelraj [Syncfusion]
Replied On February 3, 2017 01:28 AM

Hi Arda, 

Thanks for using Syncfusion products. 

We have created the simple sample with GridDataBoundGrid and in that sample , the first 3 column values are calculated and displayed it in 4th column as per your requirement. To create the dynamic formula , GridFormulaEngine can be used. In the attached sample, the cell values are calculated in CurrentCellKeyDown event when press the “enter” key. Please make use of the below code. 

Code example: 
this.gridDataBoundGrid1.CurrentCellKeyDown += new KeyEventHandler(gridDataBoundGrid1_CurrentCellKeyDown); 
 
void gridDataBoundGrid1_CurrentCellKeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Enter) 
    { 
        GridFormulaEngine engine = new GridFormulaEngine(this.gridDataBoundGrid1.Model); 
        List<MyMath> source = this.gridDataBoundGrid1.DataSource as List<MyMath>; 
 
        string cellText = this.gridDataBoundGrid1.CurrentCell.Renderer.ControlText; 
        int i = this.gridDataBoundGrid1.CurrentCell.RowIndex - 1; 
        double aValue = (double)source[i].A; 
        int bValue = (int)source[i].B; 
        double cValue = (double)source[i].C; 
 
        if (!string.IsNullOrEmpty(cellText)) 
        { 
            string formula = cellText; 
            //Replacing the given expesson with values. 
            if (formula.Contains("a")) 
                formula = formula.Replace("a", aValue.ToString()); 
            if (formula.Contains("b")) 
                formula = formula.Replace("b", bValue.ToString()); 
            if (formula.Contains("c")) 
                formula = formula.Replace("c", cValue.ToString()); 
            string result = engine.ComputeFormulaText(formula); 
 
            this.gridDataBoundGrid1.CurrentCell.Renderer.ControlText = result; 
        } 
        this.gridDataBoundGrid1.Refresh(); 
    } 
} 
 

 

 

Sample link: 

Regards, 
Piruthiviraj 


Arda Beyazoglu
Replied On February 3, 2017 02:46 AM

Thank you very much. Syncfusion is a great product.

Piruthiviraj Malaimelraj [Syncfusion]
Replied On February 6, 2017 12:00 AM

Hi Arda, 

Thanks for the update. 

We are glad to know that the provided solution is resolved your scenario. Please let us know if you have any other queries. 

Regards, 
Piruthiviraj 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;