How to perform SpellChecker in WinForms GridControl, GridGroupingControl and GridDataBoundGrid?
Spellchecker
The Essential Grid does not have direct support for SpellChecker. In order to achieve SpellChecker in Grid, you need to customize the Grid by creating the custom cell renderer and cell model. Refer to the following article link to know more about the creating cell renderer and cell model
How to create the Cell Model and Cell Renderer?
Solution:
In this article, the SpellChecker component is used to validate the strings in the cell. Based on the result, the cell text is underlined when the text is misspelled, since customization for multiple words is not provided. To know more about the SpellChecker component, you can refer to the following link: Spellchecker
Creating cell renderer and cell model:
C#
public class SpellCheckCellRenderer : GridTextBoxCellRenderer { //Using SpellChecker tool. private SpellChecker spellchecker; public SpellCheckCellRenderer(GridControlBase grid, GridCellModelBase cellModel) : base(grid, cellModel) { spellchecker = new SpellChecker(); } //To underline the word in cell that is misspelled. protected override void OnDraw(Graphics g, Rectangle clientRectangle, int rowIndex, int colIndex, GridStyleInfo style) { //Validate the cell values. string check = this.spellchecker.SpellCheck(style.CellValue.ToString()); //Checking whether the result has suggestions. if(check != "") { style.Font.Underline = true; } base.OnDraw(g, clientRectangle, rowIndex, colIndex, style); } } public class SpellCheckCellModel : GridTextBoxCellModel { ///<summary> ///Initializes a new <see cref="PercentTextBoxCellModel"/> ///</summary> ///<param name="grid">GridModel.</param> public SpellCheckCellModel(GridModel grid) : base(grid) { } /// <summary> /// Creates a cell renderer. /// </summary> /// <param name="control">GridControlBase</param> /// <returns>A new <see cref="PercentTextBoxCellRenderer"/>specific for a <see cref="GridControlBase"/></returns> public override GridCellRendererBase CreateRenderer(GridControlBase control) { return new SpellCheckCellRenderer(control, this); } }
VB
Public Class SpellCheckCellRenderer Inherits GridTextBoxCellRenderer Private spellchecker As SpellChecker Public Sub New(ByVal grid As GridControlBase, ByVal cellModel As GridCellModelBase) MyBase.New(grid, cellModel) spellchecker = New SpellChecker() End Sub ‘To underline the word in cell that is misspelled. Protected Overrides Sub OnDraw(ByVal g As Graphics, ByVal clientRectangle As Rectangle, ByVal rowIndex As Integer, ByVal colIndex As Integer, ByVal style As GridStyleInfo) If style.CellValue IsNot Nothing Then Dim check As String = Me.spellchecker.SpellCheck(style.CellValue.ToString()) 'To check whether value has suggestion. If check <> "" Then style.Font.Underline = True End If End If MyBase.OnDraw(g, clientRectangle, rowIndex, colIndex, style) End Sub End Class Public Class SpellCheckCellModel Inherits GridTextBoxCellModel '''<summary> '''Initializes a new <see cref="PercentTextBoxCellModel"/> '''</summary> '''<param name="grid">GridModel.</param> Public Sub New(ByVal grid As GridModel) MyBase.New(grid) End Sub ''' <summary> ''' Creates a cell renderer. ''' </summary> ''' <param name="control">GridControlBase</param> ''' <returns>A new <see cref="PercentTextBoxCellRenderer"/>specific for a <see cref="GridControlBase"/></returns> Public Overrides Function CreateRenderer(ByVal control As GridControlBase) As GridCellRendererBase Return New SpellCheckCellRenderer(control, Me) End Function End Class
Adding cell model and assigning CellType:
For GridControl:
C#
//Add the Custom Cell Model to Grid. this.gridControl1.CellModels.Add("SpellCheckCell", new SpellCheckCellModel(this.gridControl1.Model)); //Set the Custom Cell Type. this.gridControl1.TableStyle.CellType = "SpellCheckCell";
VB
'Add the Custom Cell Model to Grid. Me.gridControl1.CellModels.Add("SpellCheckCell", New SpellCheckCellModel(Me.gridControl1.Model)) 'Set the Custom Cell Type. Me.gridControl1.TableStyle.CellType = "SpellCheckCell"
For GridGroupingControl:
C#
//Add the Custom Cell Model to Grid. this.gridGroupingControl1.TableModel.CellModels.Add("SpellCheckCell", new SpellCheckCellModel(this.gridGroupingControl1.TableModel)); //Set the Custom Cell Type. this.gridGroupingControl1.TableDescriptor.Columns[1].Appearance.AnyRecordFieldCell.CellType = "SpellCheckCell";
VB
'Add the Custom Cell Model to Grid. Me.gridGroupingControl1.TableModel.CellModels.Add("SpellCheckCell", New SpellCheckCellModel(Me.gridGroupingControl1.TableModel)) 'Set the Custom Cell Type. Me.gridGroupingControl1.TableDescriptor.Columns(1).Appearance.AnyRecordFieldCell.CellType = "SpellCheckCell"
For GridDataBoundGrid:
C#
//Add the Custom Cell Model to Grid. this.gridDataBoundGrid1.Model.CellModels.Add("SpellCheckCell", new SpellCheckCellModel(this.gridDataBoundGrid1.Model)); //Set the Custom Cell Type. this.gridDataBoundGrid1.Binder.InternalColumns[1].StyleInfo.CellType = "SpellCheckCell";
VB
'Add the Custom Cell Model to Grid. Me.gridDataBoundGrid1.Model.CellModels.Add("SpellCheckCell", New SpellCheckCellModel(Me.gridDataBoundGrid1.Model)) 'Set the Custom Cell Type. Me.gridDataBoundGrid1.Binder.InternalColumns(1).StyleInfo.CellType = "SpellCheckCell"
Screenshot:
Figure 1: Output
Samples:
GridControl:
C#: SpellChecker_GridControl_CS
VB: SpellChecker_GridControl_VB
GridGroupingControl:
GridDataBoundGrid: