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. Image for the cookie policy date

dynamic formula using gridGroupingContro

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


5 Replies

AR Amal Raj U Syncfusion Team November 11, 2016 02:02 PM UTC

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. 



AB Arda Beyazoglu February 2, 2017 07:25 PM UTC

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.


PM Piruthiviraj Malaimelraj Syncfusion Team February 3, 2017 06:28 AM UTC

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 



AB Arda Beyazoglu February 3, 2017 07:46 AM UTC

Thank you very much. Syncfusion is a great product.


PM Piruthiviraj Malaimelraj Syncfusion Team February 6, 2017 05:00 AM UTC

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 


Loader.
Up arrow icon