Syncfusion GridControl Reading CellValue using Generic Function

I have been facing issue while accessing the cellValue method of GridControl. Each CellType has different way of returning the value of cell. To handle this I have developed a generic function which take few parameter and return the cell value as per Type required by user. 


  Public Enum CellValueReturnType

        [String] = 0

        [Number] = 1

        [Date] = 3

        [Boolean] = 4

        [Time] = 5

        [FormatText] = 6

    End Enum

 

 

Public Shared Function GetGridCellValue(ByVal GrdCtl As ARCGridControl, ByVal RowIndex As Integer, ByVal ColIndex As Integer, ByVal ReturnType As CellValueReturnType) As Object

 

        Dim CellValue As Object = Nothing

 

        If RowIndex <> 0 Then

            If GrdCtl.Item(RowIndex, ColIndex).CellType Is "MaskEdit" Then

                If InStr(GrdCtl.Item(RowIndex, ColIndex).FormattedText, ".") Then

                    CellValue = Val(GrdCtl.Item(RowIndex, ColIndex).FormattedText)

                Else

 

                    If GrdCtl.Item(RowIndex, ColIndex).MaskEdit.Mask = "99/99/9999" OrElse GrdCtl.Item(RowIndex, ColIndex).MaskEdit.Mask = "99/99/99" Then

                        If IsDate(GrdCtl.Item(RowIndex, ColIndex).FormattedText) = True Then

                            CellValue = GrdCtl.Item(RowIndex, ColIndex).FormattedText

                        End If

 

                    ElseIf ReturnType = CellValueReturnType.Date AndAlso GrdCtl.Item(RowIndex, ColIndex).FormattedText.ToString <> String.Empty Then

                        CellValue = GrdCtl.Item(RowIndex, ColIndex).FormattedText  'Return Value Entered by User

 

                    ElseIf ReturnType = CellValueReturnType.Time AndAlso GrdCtl.Item(RowIndex, ColIndex).FormattedText.ToString <> String.Empty Then

                        If GrdCtl.Item(RowIndex, ColIndex).FormattedText.ToString.Trim <> ":" Then

                            CellValue = GrdCtl.Item(RowIndex, ColIndex).FormattedText 

                        End If

 

                    End If

                End If

 

            ElseIf GrdCtl.Item(RowIndex, ColIndex).CellType Is "ComboBox" Or GrdCtl.Item(RowIndex, ColIndex).CellType Is "GridListControl" Then

                If IsNothing(GrdCtl.Item(RowIndex, ColIndex).CellValue) = False AndAlso GrdCtl.Item(RowIndex, ColIndex).CellValue.ToString <> "" Then

                                        If ReturnType = CellValueReturnType.FormatText Then

                        CellValue = GrdCtl.Item(RowIndex, ColIndex).FormattedText

                    Else

                        CellValue = GrdCtl.Item(RowIndex, ColIndex).CellValue

                    End If

 

                End If

 

            ElseIf GrdCtl.Item(RowIndex, ColIndex).CellType Is "CheckBox" Then

                If GrdCtl.Item(RowIndex, ColIndex).CellValue.ToString.Length <> 0 AndAlso CBool(GrdCtl.Item(RowIndex, ColIndex).CellValue) = True Then

                    CellValue = True

                Else

                    CellValue = False

                End If

 

            ElseIf GrdCtl.Item(RowIndex, ColIndex).CellType Is "Currency" Then 'To Remove the Comma from CellValue

                If IsNothing(GrdCtl.Item(RowIndex, ColIndex).CellValue) = False AndAlso IsDBNull(GrdCtl.Item(RowIndex, ColIndex).CellValue) = False AndAlso GrdCtl.Item(RowIndex, ColIndex).CellValue.ToString <> "" Then

                    If GrdCtl.Item(RowIndex, ColIndex).CellValue <> 0 Then

                        CellValue = Convert.ToDecimal(GrdCtl.Item(RowIndex, ColIndex).CellValue)

                    End If

                End If

 

            ElseIf GrdCtl.Item(RowIndex, ColIndex).CellType Is "FormulaCell" Then

                If IsNothing(GrdCtl.Item(RowIndex, ColIndex).FormattedText) = False Then

                    CellValue = IIf(GrdCtl.Item(RowIndex, ColIndex).FormattedText = String.Empty, 0, GrdCtl.Item(RowIndex, ColIndex).FormattedText)

                End If

            Else

                If GrdCtl.Item(RowIndex, ColIndex).CellValue IsNot Nothing AndAlso GrdCtl.Item(RowIndex, ColIndex).CellValue.ToString <> "" Then

                    CellValue = GrdCtl.Item(RowIndex, ColIndex).CellValue.ToString

                End If

            End If

        End If

 

        If IsNothing(CellValue) = True Then

            If ReturnType = CellValueReturnType.Number Then

                Return 0

 

            ElseIf ReturnType = CellValueReturnType.Boolean Then                 Return False

            Else

                Return ""

            End If

        Else

            Return CellValue

        End If

 

    End Function

Comments

Popular posts from this blog

Rebuild Index

Exclude property of Class Object from Serialization

Multiple Connection from Config File