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

Gridview validation and error marking/coloring

1 Answer 215 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Donald
Top achievements
Rank 1
Donald asked on 27 Jul 2011, 09:30 PM
hi, i have looked at the other posts regarding validation and your validation video. i do understand the row_validation and cell_validation events. However, in order to validate the entries AND mark the errors, i have to use the validation code twice which i dont want to do.  

My test code is shown below. Have also attached a screenshot.

Here's what i am trying to do:

in the grid2_RowValidating event i am validating and preventing user to go to next row. i would like to "mark/color" the cell with the error. I tried all the Style properties but only the forecolor works. (i have commented the lines not working)

So, in addition to RowValidating event, i decided to use the grid2_ViewCellFormatting event to validate and color the cell with the error.
This is working but i dont understand why i have to validate twice. (once in the RowValidating event to prevent them to go to the next row and again in the ViewCellFormatting event to "color" the cell) 

Am i doing something wrong? Why am i not able to set the Style Properties directly from within the RowValidating. I am using the Q2 2011 version (latest) 

Private Sub grid2_RowValidating(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.RowValidatingEventArgs) Handles grid2.RowValidating
    If TypeOf e.Row Is GridViewNewRowInfo Or TypeOf e.Row Is GridViewDataRowInfo Then
        If Not e.Row Is Nothing Then
                              
            Dim value As String = e.Row.Cells(1).Value
            If value > 10 Then
                e.Cancel = True
                e.Row.Cells(1).ErrorText = "cannot be greater than 10"
                e.Row.ErrorText = "cannot be greater than 10"
                e.Row.Cells(1).Style.ForeColor = Color.Red
                'These did not work so had to use the cell formating
                'e.Row.Cells(1).Style.DrawFill = True
                'e.Row.Cells(1).Style.NumberOfColors = 1
                'e.Row.Cells(1).Style.BackColor = Color.Peru
 
                'e.Row.Cells(1).Style.DrawBorder = True
                'e.Row.Cells(1).Style.BorderWidth = 2
            Else
                e.Row.ErrorText = String.Empty
            End If
        End If
    End If
 
 
End Sub
 
Private Sub grid2_ViewCellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles grid2.ViewCellFormatting
    If grid2.IsInEditMode Then
 
        If TypeOf e.Row Is GridViewRowInfo Then
 
            Select Case e.ColumnIndex
                Case 1
                    e.CellElement.DrawFill = True
                    e.CellElement.NumberOfColors = 1
 
                    'validate again?? to set the coloring?
                    If e.CellElement.Value > 10 Then
                        'set visual clue
                        e.CellElement.BackColor = Color.Peru
                        e.CellElement.ForeColor = Color.Yellow
                        e.CellElement.Image = My.Resources._error
                    Else
                        'reset all
                        e.CellElement.BackColor = Color.White
                        e.CellElement.ForeColor = Color.Black
                        e.CellElement.Image = Nothing
                    End If
            End Select
 
        End If
    End If
End Sub
 

1 Answer, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 01 Aug 2011, 02:15 PM
Hi Donald,

RadGridView uses UI virtualization for its elements and because of this the preferred way to customize its cells is the CellFormatting event. The Style property has limited formatting capabilities and it is useful only when formatting specific cells. 

In order to customize the fill and the border you should set the CustomizeFill and CustomizeBorder properties to true. The DrawFill and DrawBorder properties on the other side enable or disable the fill and border paint routines. I modified your code the following way:
Private Sub grid2_RowValidating(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.RowValidatingEventArgs) Handles grid2.RowValidating
    If TypeOf e.Row Is GridViewNewRowInfo Or TypeOf e.Row Is GridViewDataRowInfo Then
        If Not e.Row Is Nothing Then                                         
            Dim value As String = e.Row.Cells(1).Value
            If value > 10 Then
                e.Cancel = True
                e.Row.Cells(1).ErrorText = "cannot be greater than 10"
                e.Row.ErrorText = "cannot be greater than 10"
                e.Row.Cells(1).Style.ForeColor = Color.Red
                'These did not work so had to use the cell formating
                e.Row.Cells(1).Style.CustomizeFill = True
                e.Row.Cells(1).Style.DrawFill = True
                e.Row.Cells(1).Style.NumberOfColors = 1
                e.Row.Cells(1).Style.GradientStyle =  GradientStyles.Solid
                e.Row.Cells(1).Style.BackColor = Color.Peru
  
                e.Row.Cells(1).Style.CustomizeBorder = True
                e.Row.Cells(1).Style.DrawBorder = True
                e.Row.Cells(1).Style.BorderWidth = 2
                e.Row.Cells(1).Style.BorderGradientStyle = GradientStyles.Solid
                e.Row.Cells(1).Style.BorderColor = Color.Red           
            Else
                e.Row.ErrorText = String.Empty
            End If
        End If
    End If
End Sub

If you have any further questions, do not hesitate to ask.
 
All the best,
Jack
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
GridView
Asked by
Donald
Top achievements
Rank 1
Answers by
Jack
Telerik team
Share this question
or