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 row and formula

Thread ID:





128989 Feb 20,2017 09:32 AM Feb 21,2017 02:16 AM Windows Forms 1
Tags: GridControl
Asked On February 20, 2017 09:32 AM

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

Piruthiviraj Malaimelraj [Syncfusion]
Replied On February 21, 2017 02:16 AM

Hi Slava, 

Thanks for your interest in Syncfusion products. 

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(); 
                case 3: 
                    col3 = this.gridControl1.Model[i, j].CellValue.ToString(); 
                case 4: 
                    col4 = this.gridControl1.Model[i, j].CellValue.ToString(); 
        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; 
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: 



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.