How do I display an error icon in the row header when the datarow has a row error?
To display an error icon in the rowheader that reflects the DataRow's error, you need to handle couple of events - PrepareViewStyleInfo and CellDrawn event. Please follow these steps:
1) In the PreapreViewStyleInfo eventhandler, get the underlying DataRow's RowError and set the CellTipText and the GridStyleInfo's Tag property.
C#
/// The Event is raised to allow custom formatting of the cell by changing it's style object just before it is drawn
private void gridDataBoundGrid1_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e)
{
if(e.RowIndex >= 1 && e.ColIndex == 0 && e.RowIndex < this.gridDataBoundGrid1.Model.RowCount)
{
//converts the absolute rowindex to Zero-based postion in the same grid
int pos = this.gridDataBoundGrid1.Binder.RowIndexToPosition(e.RowIndex);
DataRow row = dt.Rows[ pos ];
if (row.RowError != null && row.RowError != "")
{
//Tooltip text to be displayed when user moves mouse over cell
e.Style.CellTipText = row.RowError;
e.Style.Tag = row.RowError;
}
}
}
VB
''' The Event is raised to allow custom formatting of the cell by changing it's style object just before it is drawn
Private Sub gridDataBoundGrid1_PrepareViewStyleInfo(ByVal sender As Object, ByVal e As GridPrepareViewStyleInfoEventArgs)
If e.RowIndex > = 1 AndAlso e.ColIndex = 0 AndAlso e.RowIndex < Me.gridDataBoundGrid1.Model.RowCount Then
'converts the absolute rowindex to Zero-based postion in the same grid
Dim pos As Integer = Me.gridDataBoundGrid1.Binder.RowIndexToPosition(e.RowIndex)
Dim row As DataRow = dt.Rows(pos)
If Not row.RowError Is Nothing AndAlso row.RowError >< "" Then
'Tooltip text to be displayed when user moves mouse over cell
e.Style.CellTipText = row.RowError
e.Style.Tag = row.RowError
End If
End If
End Sub
2) In the CellDrawn eventhandler, draw the error icon in the row header checking if the GridStyleInfo's tag has a valid string.
C#
/// Occurs for every cell after the grid has drawn the specified cell
private void gridDataBoundGrid1_CellDrawn(object sender, GridDrawCellEventArgs e)
{
if(e.RowIndex >= 1 && e.RowIndex < this.gridDataBoundGrid1.Model.RowCount && e.ColIndex == 0 && e.Style.Tag is string)
{
//designs the Rectangle for drawing the icon
Rectangle iconBounds = Rectangle.FromLTRB(e.Bounds.Right-15, e.Bounds.Top, e.Bounds.Right, e.Bounds.Bottom);
// draws the Icon inside the specified Rectangle
e.Graphics.DrawIcon(new ErrorProvider().Icon,iconBounds);
}
}
VB
''' Occurs for every cell after the grid has drawn the specified cell
Private Sub gridDataBoundGrid1_CellDrawn(ByVal sender As Object, ByVal e As GridDrawCellEventArgs)
If e.RowIndex >= 1 AndAlso e.RowIndex < Me.gridDataBoundGrid1.Model.RowCount AndAlso e.ColIndex = 0 AndAlso TypeOf e.Style.Tag Is String Then
'designs the Rectangle for drawing the icon
Dim iconBounds As Rectangle = Rectangle.FromLTRB(e.Bounds.Right-15, e.Bounds.Top, e.Bounds.Right, e.Bounds.Bottom)
' draws the Icon inside the specified Rectangle
e.Graphics.DrawIcon(New ErrorProvider().Icon,iconBounds)
End If
End Sub
Here is a sample that illustrates this:
http://websamples.syncfusion.com/samples/KB/Grid.Windows/TestErrorProvider/main.htm