BoldDeskBoldDesk is now live on Product Hunt with a special offer: 50% off all plans. Let's grow together! Support us.
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+")"; } } |