How to display custom error icon in WinForms GridGroupingControl?
Display custom error icon
To draw an error icon at the left side of the cell text rectangle, use the CustomCellRenderer and CustomCellModel for GridTextBoxCellRenderer and GridTextBoxCellModel respectively. Then, override the OnDraw method in CustomCellRenderer.
Creating Cell Model
C#
public class CustomCellModel : GridTextBoxCellModel { public CustomCellModel(GridModel grid) : base(grid) { } public override GridCellRendererBase CreateRenderer(GridControlBase control) { return new CustomCellRenderer(control, this); } }
VB
Public Class CustomCellModel Inherits GridTextBoxCellModel Public Sub New(ByVal grid As GridModel) MyBase.New(grid) End Sub Public Overrides Function CreateRenderer(ByVal control As GridControlBase) As GridCellRendererBase Return New CustomCellRenderer(control, Me) End Function End Class
Creating Cell Renderer
C#
public class CustomCellRenderer : GridTextBoxCellRenderer { public CustomCellRenderer(GridControlBase grid, GridCellModelBase cellModel) : base(grid, cellModel) { } protected override void OnDraw(Graphics g, Rectangle clientRectangle, int rowIndex, int colIndex, GridStyleInfo style) { if (this.Grid.CurrentCell.HasCurrentCellAt(rowIndex, colIndex) && this.Grid.CurrentCell.IsEditing && !this.Grid.CurrentCell.IsValid) { Rectangle rect = clientRectangle; Bitmap bitmap = SystemIcons.Error.ToBitmap(); rect = GridUtil.CenterInRect(rect, new Size(20, 20)); g.DrawImage(bitmap, new Rectangle(clientRectangle.X, rect.Y, bitmap.Width -10, rect.Height)); base.OnDraw(g, new Rectangle(clientRectangle.X + bitmap.Width, clientRectangle.Y, clientRectangle.Width - bitmap.Width, clientRectangle.Height), rowIndex, colIndex, style); } else base.OnDraw(g, clientRectangle, rowIndex, colIndex, style); } }
VB
Public Class CustomCellRenderer Inherits GridTextBoxCellRenderer Public Sub New(ByVal grid As GridControlBase, ByVal cellModel As GridCellModelBase) MyBase.New(grid, cellModel) End Sub 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 Me.Grid.CurrentCell.HasCurrentCellAt(rowIndex, colIndex) AndAlso Me.Grid.CurrentCell.IsEditing AndAlso (Not Me.Grid.CurrentCell.IsValid) Then Dim rect As Rectangle = clientRectangle Dim bitmap As Bitmap = SystemIcons.Error.ToBitmap() rect = GridUtil.CenterInRect(rect, New Size(20, 20)) g.DrawImage(bitmap, New Rectangle(clientRectangle.X, rect.Y, bitmap.Width - 10, rect.Height)) MyBase.OnDraw(g, New Rectangle(clientRectangle.X + bitmap.Width, clientRectangle.Y, clientRectangle.Width - bitmap.Width, clientRectangle.Height), rowIndex, colIndex, style) Else MyBase.OnDraw(g, clientRectangle, rowIndex, colIndex, style) End If End Sub End Class
Adding CellModel and Assigning CellType
C#
this.gridGroupingControl1.TableControl.CurrentCell.ShowErrorIcon = false; this.gridGroupingControl1.TableModel.CellModels.Add("CustomCell", new CustomCellModel(this.gridGroupingControl1.TableModel)); this.gridGroupingControl1.TableDescriptor.Appearance.AnyRecordFieldCell.CellType = "CustomCell";
VB
Me.gridGroupingControl1.TableControl.CurrentCell.ShowErrorIcon = False Me.gridGroupingControl1.TableModel.CellModels.Add("CustomCell", New CustomCellModel(Me.gridGroupingControl1.TableModel)) Me.gridGroupingControl1.TableDescriptor.Appearance.AnyRecordFieldCell.CellType = "CustomCell"
Samples:
I hope you enjoyed learning about how to display custom error icon at the left side of the cell in WinForms GridGroupingControl.
You can refer to our WinForms GridControl feature
tour page to learn about its other groundbreaking
feature representations. You can also explore our
WinForms GridControl documentation to
understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!