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
close icon

Dynamic row and formula

Hello. Sorry for bad english.

GridControl There where 6 columns.
Lines add it by doing: gridControl.Rows.InsertRange (1, 1);

How to make that happen in each line adding value from column 2 to column 4, and displays this in the 6 column.
It turns out only to make the addition of all the lines, and it is necessary that the sum of findings for each separate line

1 Reply

PM Piruthiviraj Malaimelraj Syncfusion Team February 21, 2017 07:16 AM UTC

Hi Slava, 

Thanks for your interest in Syncfusion products. 

Query 
Response 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
How to make that happen in each line adding value from column 2 to column 4, and displays this in the 6 column. 
Suggestion 1: 
To perform addition of the particular column values and displayed in a another column, set the CellType of that column as FormulaCell and enter the valid formula in that cell as in below image, 

Code example: 
this.gridControl1.Model.ColStyles[6].CellType = GridCellTypeName.FormulaCell; 

 
Suggestion 2: 
To  perform arithmetic operations based on the particular values by providing 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.gridControl1.CurrentCellKeyDown += new KeyEventHandler(gridControl1_CurrentCellKeyDown); 
 
void gridControl1_CurrentCellKeyDown(object sender, KeyEventArgs e) 
    if (e.KeyCode == Keys.Enter) 
    { 
        GridFormulaEngine formulaEngine = new GridFormulaEngine(this.gridControl1.Model); 
        string cellvalue = this.gridControl1.CurrentCell.Renderer.ControlText; 
        int i = this.gridControl1.CurrentCell.RowIndex; 
        string col2 = "", col3 = "", col4 = ""
        for (int j = 2; j <= 4; j++) 
        { 
            switch (j) 
            { 
                case 2: 
                    col2 = this.gridControl1.Model[i, j].CellValue.ToString(); 
                    continue; 
                case 3: 
                    col3 = this.gridControl1.Model[i, j].CellValue.ToString(); 
                    continue; 
                case 4: 
                    col4 = this.gridControl1.Model[i, j].CellValue.ToString(); 
                    break
            } 
        } 
        if (cellvalue != null
        { 
            string formula = cellvalue.ToString(); 
            //Replacing the given expesson with values. 
            if (formula.Contains("b")) 
                formula = formula.Replace("b", col2.ToString()); 
            if (formula.Contains("c")) 
                formula = formula.Replace("c", col3.ToString()); 
            if (formula.Contains("d")) 
                formula = formula.Replace("d", col4.ToString()); 
            string result = formulaEngine.ComputeFormulaText(formula); 
 
            this.gridControl1.CurrentCell.Renderer.ControlText = result; 
        } 
        this.gridControl1.Refresh(); 
    } 
 
 
Suggestion 3: 
If you want to perform addition operation while in Form_Load and also for added records at run time, QueryCellInfo event can be used. Please make use of the below code, 

Code example: 
this.gridControl1.QueryCellInfo += new GridQueryCellInfoEventHandler(gridControl1_QueryCellInfo); 

void gridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e) 
    if (e.ColIndex == 6 && e.RowIndex >= this.gridControl1.TopRowIndex) 
    { 
        int r = e.RowIndex; 
        string parseText = "b" + r + "," + "c" + r + "," + "d" + r; 
 
        e.Style.CellType = "FormulaCell"; 
        e.Style.CellValue = "=Sum("+parseText+")"; 
    } 
 
 

Sample link: 

Regards, 
Piruthiviraj

Loader.
Live Chat Icon For mobile
Up arrow icon