The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Infragistics UltraWinGrid and DBNull-Values

Last post 09-08-2008, 8:46 AM by jhotchkiss. 6 replies.
Sort Posts: Previous Next
  •  09-29-2007, 6:18 AM 5379

    Infragistics UltraWinGrid and DBNull-Values

    EntitySpaces Version # 2007.0.0730.0
    MyGeneration Version # 1.2.0.7

    Language: VB.NET

    Using the Infragistics WinGrid with EntitySpaces leads sometimes to let's say interesting issues. Using VB.NET does the rest... Wink

    Bounding an EntityCollection to WinGrid and deleting the value of a cell, results in a DBNull-Value for that cell. Since in VB we need the value Nothing, you get permanent errors like "Unable to update the data value: The object with the type System.DBNull can not be converted to the type System.String.". (I've tried to translate the message...)

    To get around this issue, you have to switch DBNull into Nothing.

    Code:
        Private _DBNullToNothingCell As UltraGridCell = Nothing
    
        Private Sub uxGrid_BeforeCellUpdate(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.BeforeCellUpdateEventArgs) Handles uxGrid.BeforeCellUpdate
            If IsDBNull(e.NewValue) Then _DBNullToNothingCell = e.Cell
        End Sub
    
        Private Sub uxGrid_CellDataError(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.CellDataErrorEventArgs) Handles uxGrid.CellDataError
            If _DBNullToNothingCell IsNot Nothing Then
                e.RaiseErrorEvent = False
                e.RestoreOriginalValue = False
                e.StayInEditMode = False
                _DBNullToNothingCell.Value = Nothing
                _DBNullToNothingCell = Nothing
            End If
        End Sub
    

     

    The reason why I use the variable DBNullToNothingCell and not ActiveCell in CellDataError is, that in some circumstances ActiveCell ist not always the same. Furthermore it is not possible to change the value of the cell in BeforeCellUpdate. That's why I'm doing it in the CellDataError-Event.

    I'm sure there is a more elegant solution out there, and I'm definitly interested in it. Maybe I've missed something in ES, so that I could avoid using these events. But on the other hand, if some else is just struggling with this issue, here are my 2 Cents how you can deal with it.

    BTW, I've just started using EntitySpaces again for a current project - after using it on the same project about one year ago. I really have to say that it is a lot of fun working with it again. I think you - the ES Team - are really doing a great job.

    Thanks for a great product!

     

  •  09-30-2007, 1:57 AM 5389 in reply to 5379

    Re: Infragistics UltraWinGrid and DBNull-Values

    Hmm, after reading the following post
    Possible Defect: Updating to blank string in grid using esDataSource
    I wonder if it shouldn't also be possible with the Infragistics Grid to get it to work without the events mentioned above?

    Infragistics Grid seems to implicitly use the underlying type System.String, which doesn't allow DBNull but Nothing. The error gets fired by the Grid itself. So the following code in ES wouldn't be called in this case:

     
    Code:
        Public Overrides Sub SetProperty(name as string, value as object)
                
                    If Not Me.Row Is Nothing Then
           
                        Dim col As esColumnMetadata = Me.Meta.Columns.FindByPropertyName(name)
                        If Not col Is Nothing Then
                        
                           If value Is Nothing OrElse value.GetType().ToString() = "System.String" Then
    
    
     
    Of course it does work with datasets without any problems. How can I get it to work with ES?
  •  09-30-2007, 2:22 AM 5390 in reply to 5389

    Re: Infragistics UltraWinGrid and DBNull-Values

    I've forgotten to mention, that I've tried this:

    Code:
      Public Overrides Sub SetProperty(name as string, value as object)
    If Me.Row Is Nothing Then
    Me.AddNew() End IF Dim col As esColumnMetadata = Me.Meta.Columns.FindByPropertyName(name) If Not col Is Nothing Then If IsDBNull(value) Then value = Nothing If value Is Nothing OrElse value.GetType().ToString() = "System.String" Then

     

    But since the Grid checks the data type by itself the error gets thrown there and the code above is not called anyway.

    Nevertheless if the value equals DBNull in VB, I think changing it to Nothing in SetProperty makes sense, doesn't it? Unless I've completely missed something maybe this should be done in your VB templates?

    BTW, you could improve your generated VB code by generally using AndAlso instead of And and OrElse instead of Or.

  •  10-13-2007, 2:30 PM 5838 in reply to 5390

    Re: Infragistics UltraWinGrid and DBNull-Values

    Oh no, it's so easy...!

    Just use the following code (for example in the InitializeLayout Event):

    Code:
    For Each column As UltraGridColumn In grid.Rows.Band.Columns
        column.Nullable = Nullable.Nothing
    Next
     

    With the Nullable property you can define how UltraGrid store Null values. The list below shows all possible values for Nullable:

    Member Description
    Automatic Automatic. Data stored as null if allowed, otherwise as empty string.
    Null DBNull. Data stored as DBNull value.
    EmptyString Empty String. Data stored as a zero-length string.
    Nothing Nothing. Data stored as Nothing (C# null).
    Disallow Column doesn't allow empty values.

    Nevertheless there is a need to react on MultiCellOperations. In case of a MultiCellOperation like Cut, Copy or Delete, UltraGrid seems not to use the Nullable property. Here's my solution for that scenario:

    Code:
    Private Sub uxGrid_BeforeMultiCellOperation(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.BeforeMultiCellOperationEventArgs) Handles uxtGrid.BeforeMultiCellOperation
        Dim isPasteOperation As Boolean = (e.Operation = MultiCellOperation.Paste)
        If e.Operation = MultiCellOperation.Delete OrElse e.Operation = MultiCellOperation.Cut OrElse isPasteOperation Then
            For rowIndex As Integer = 0 To e.Cells.RowCount - 1
                For colIndex As Integer = 0 To e.Cells.ColumnCount - 1
                    If isPasteOperation Then
                        If IsDBNull(e.NewValues(e.Cells.Item(rowIndex, colIndex)).Value) OrElse _
                           e.NewValues(e.Cells.Item(rowIndex, colIndex)).Value.ToString = "" Then
                            e.NewValues(e.Cells.Item(rowIndex, colIndex)).Value = Nothing
                        End If
                    Else
                        e.NewValues(e.Cells.Item(rowIndex, colIndex)).Value = Nothing
                    End If
                Next
            Next
         End If
    End Sub

    Again I could imagine that there exists a better solution. But I couldn't find any, so maybe this can be also usefull for others using the UltraGrid.

  •  12-29-2007, 3:08 PM 7366 in reply to 5838

    Re: Infragistics UltraWinGrid and DBNull-Values

    I appreciate the post, this has certainly saved me some time digging for an answer.

  •  01-28-2008, 6:02 AM 7819 in reply to 7366

    Re: Infragistics UltraWinGrid and DBNull-Values

    I'm glad it was of any help for you. Smile

    Thanks for your feedback!

  •  09-08-2008, 8:46 AM 11230 in reply to 7819

    Re: Infragistics UltraWinGrid and DBNull-Values

    Nice one! Saved me a lot of arsing around with that.

     


    Jim
View as RSS news feed in XML