Category / Section
How to make 'CTRL+~', shortcut key, to show formulas instead of values similar to Excel in the WinForms GridControl?
1 min read
Show the formulas instead of values in grid
This can be done by handling the GridControl's KeyDown and DrawCellDisplayText event. A flag can be used to synchronize the KeyDown event and the DrawCellDisplayText for displaying the formula.
C#
this.gridControl1.KeyDown += gridControl1_KeyDown; //set the cellType as FormulaCell this.gridControl1.TableStyle.CellType = GridCellTypeName.FormulaCell; this.gridControl1.DrawCellDisplayText += gridControl1_DrawCellDisplayText; void gridControl1_DrawCellDisplayText(object sender, GridDrawCellDisplayTextEventArgs e) { if (this.gridControl1[e.Style.CellIdentity.RowIndex, e.Style.CellIdentity.ColIndex].CellType == "FormulaCell" & flag) { //draw the display text for the cell e.DisplayText = e.Style.CellValue.ToString(); } } void gridControl1_KeyDown(object sender, KeyEventArgs e) { bool keydown = (e.Modifiers & Keys.Control) != Keys.None; Keys Key = e.KeyCode & Keys.KeyCode; //check whether the pressed key is Control with tilde or not if (Key == Keys.Oemtilde && keydown) { flag = !flag; this.gridControl1.Refresh(); this.gridControl1.Model.Refresh(); } }
VB
Private Me.gridControl1.KeyDown += AddressOf gridControl1_KeyDown 'set the cellType as FormulaCell Me.gridControl1.TableStyle.CellType = GridCellTypeName.FormulaCell AddHandler Me.gridControl1.DrawCellDisplayText, AddressOf gridControl1_DrawCellDisplayText void gridControl1_DrawCellDisplayText(Object sender, GridDrawCellDisplayTextEventArgs e) If Me.gridControl1(e.Style.CellIdentity.RowIndex, e.Style.CellIdentity.ColIndex).CellType Is "FormulaCell" & flag Then 'draw the display text for the cell e.DisplayText = e.Style.CellValue.ToString() End If void gridControl1_KeyDown(Object sender, KeyEventArgs e) Dim keydown As Boolean = (e.Modifiers And Keys.Control) <> Keys.None Dim Key As Keys = e.KeyCode And Keys.KeyCode 'check whether the pressed key is Control with tilde or not If Key = Keys.Oemtilde AndAlso keydown Then flag = Not flag Me.gridControl1.Refresh() Me.gridControl1.Model.Refresh() End If
Samples:
C#: Showing_Formulas
VB: Showing_Formulas