This is a migrated thread and some comments may be shown as answers.

ErrorProvider Icon in RADGridViewCell

2 Answers 234 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Theo
Top achievements
Rank 2
Theo asked on 22 Sep 2011, 02:41 PM
After searching the forums for some time (days, well 2 days) I finally have a solution on displaying an Error Icon in a cell of a RADGridView i.e. like in SQL Management Studio when a certain criteria is met.
Scenario 1: Validate all cells in a row simultaneously
Scenario 2: Validate an individual cell

So here goes:
Solution 1: Validate all cells in a row simultaneously
Private Sub rgvSrcExtractFiles_RowValidating(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.RowValidatingEventArgs) Handles rgvSrcExtractFiles.RowValidating
    If TypeOf e.Row Is GridViewDataRowInfo Then
        For Each cell As GridViewCellInfo In e.Row.Cells
            Select Case cell.ColumnInfo.Name
                Case "FileDirectory"
                    If Not FileAcc.DirectoryExists(cell.Value) Then
                        cell.ErrorText = "Directory not found!"
                    ElseIf FileAcc.CountFilePrefixWithExt(cell.Value, e.Row.Cells("FilePrefix").Value, e.Row.Cells("FileExtension").Value) = 0 Then
                        cell.ErrorText = "No valid files found in directory!"
                    Else
                        cell.ErrorText = String.Empty
                    End If
                Case "FilePrefix"
                    If FileAcc.CountFilePrefix(e.Row.Cells("FileDirectory").Value, cell.Value) = 0 Then
                        cell.ErrorText = "Files not found!"
                    Else
                        cell.ErrorText = String.Empty
                    End If
                Case "FileExtension"
                    If FileAcc.CountFileExt(e.Row.Cells("FileDirectory").Value, cell.Value) = 0 Then
                        cell.ErrorText = "Files not found!"
                    Else
                        cell.ErrorText = String.Empty
                    End If
            End Select
        Next
    End If
End Sub



Solution 2: Validate an individual cell

Private Sub rgvSrcExtractFiles_CellValidating(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellValidatingEventArgs)  Handles rgvSrcExtractFiles.CellValidating
    Dim column As GridViewDataColumn = TryCast(e.Column, GridViewDataColumn)
    If TypeOf e.Row Is GridViewDataRowInfo AndAlso column IsNot Nothing Then
        Select Case column.Name
            Case "FileDirectory"
                If Not FileAcc.DirectoryExists(e.Value) Then
                    DirectCast(e.Row.Cells(e.ColumnIndex), GridViewCellInfo).ErrorText = "Directory not found!"
                ElseIf FileAcc.CountFilePrefixWithExt(e.Value, e.Row.Cells("FilePrefix").Value, e.Row.Cells("FileExtension").Value) = 0 Then
                    DirectCast(e.Row.Cells(e.ColumnIndex), GridViewCellInfo).ErrorText = "Files not found!"
                Else
                    DirectCast(e.Row.Cells(e.ColumnIndex), GridViewCellInfo).ErrorText = String.Empty
                End If
            Case "FilePrefix"
                If FileAcc.CountFilePrefix(e.Row.Cells("FileDirectory").Value, e.Value) = 0 Then
                    DirectCast(e.Row.Cells(e.ColumnIndex), GridViewCellInfo).ErrorText = "Files not found!"
                Else
                    DirectCast(e.Row.Cells(e.ColumnIndex), GridViewCellInfo).ErrorText = String.Empty
                End If
            Case "FileExtension"
                If FileAcc.CountFileExt(e.Row.Cells("FileDirectory").Value, e.Value) = 0 Then
                    DirectCast(e.Row.Cells(e.ColumnIndex), GridViewCellInfo).ErrorText = "Files not found!"
                Else
                    DirectCast(e.Row.Cells(e.ColumnIndex), GridViewCellInfo).ErrorText = String.Empty
                End If
        End Select
    End If
End Sub



AND TADA :                                                                        **** THE MAGIC ***** 

Private Sub rgvSrcExtractFiles_CellFormatting(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles rgvSrcExtractFiles.CellFormatting
    Dim cell As GridDataCellElement = TryCast(e.CellElement, GridDataCellElement)
    If cell IsNot Nothing Then
        If cell.ContainsErrors Then
            'Uncomment to draw a red border around cell as well
            'cell.DrawBorder = True
            'cell.BorderBoxStyle = BorderBoxStyle.SingleBorder
            'cell.BorderWidth = 2
            'cell.BorderColor = Color.Red
            'cell.ImageAlignment = ContentAlignment.MiddleRight
 
            Dim prov As New ErrorProvider
            Dim conv As New ImageConverter
            cell.Image = conv.ConvertFrom(prov.Icon)
 
            cell.TextImageRelation = TextImageRelation.TextBeforeImage
            cell.ImageAlignment = ContentAlignment.MiddleRight
            cell.ImageLayout = ImageLayout.None
        Else
            'Resets the borders to the original value
            'cell.ResetValue(LightVisualElement.DrawBorderProperty, ValueResetFlags.Local)
            'cell.ResetValue(LightVisualElement.BorderBoxStyleProperty, ValueResetFlags.Local)
            'cell.ResetValue(LightVisualElement.BorderWidthProperty, ValueResetFlags.Local)
            'cell.ResetValue(LightVisualElement.BorderColorProperty, ValueResetFlags.Local)
 
            cell.Image = Nothing
        End If
    End If
End Sub

That's it folks. You can use EITHER Solution 1 or 2, but you have to include the magic irrespective of which one you choose.
Hope this saves you some hours. 

Sharing is Caring
The_O

2 Answers, 1 is accepted

Sort by
0
Theo
Top achievements
Rank 2
answered on 22 Sep 2011, 08:10 PM
Please leave a reply with your thoughts. Any ideas or criticism will be welcome. Thx 
0
Stefan
Telerik team
answered on 27 Sep 2011, 02:40 PM
Hi Theo,

Thank you for sharing your validation and visualization logic with the community. 

To all interested, more information regarding validation, can be found in the following articles:
- Validation
- Data Validation
- Validation in RadGridview Video
- In our Demo application under RadGridView >> Manupulate Data >> Validate Changes

And here you can see how to indicate the errors from the validation:
- In our Demo application under RadGridView >> Manupulate Data >> Indicate errors

Kind regards,
Stefan
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Theo
Top achievements
Rank 2
Answers by
Theo
Top achievements
Rank 2
Stefan
Telerik team
Share this question
or