Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
615
Removing Multiple Rows in Code via UltraDataSource
posted

Hi All:

     I have the following code behind a button's click event, which removes records from my UltraDataSource (bound to my UltraGrid) if the "Age" field is zero (or null):

 

        Dim uRow As UltraDataRow

            For Each uRow In UltraDataSource1.Rows
                If  (uRow("Age") Is DBNull.Value OrElse DirectCast(uRow("Age"), Integer) = 0) Then
                    'Delete the row and raise the Event
                    UltraDataSource1.Rows.Remove(uRow, True)
                End If
            Next

 

     The problem is that it deletes every OTHER row.  If I enter "0" for the Age in rows 1, 2, and 3, rows 1 and 3 will be deleted but row 2 will not.  If I enter "0" for the age in rows 1, 3, and 5, all three rows are deleted successfully.

     Any reason why it won't delete consecutive rows?

Kevin

  • 12773
    posted


    Hello Kevin ,

    Here is a smple code how to remove items form ultradataSource. Fireing the event RowDeleted and
    and removing the item form external data will be sinchronized in this way:
    Me.externalData.Rows.RemoveAt(e.Row.Index)

    Private Sub button1_Click(sender As Object, e As EventArgs)
        Dim rowItemsToRemove As New List(Of UltraDataRow)()
            For Each item As UltraDataRow In ultraDataSource1.Rows
            Dim value As Object = Me.ultraDataSource1.Rows(item.Index).GetCellValue(2)
            Dim intValue As Integer

            If TypeOf value Is System.DBNull OrElse (Int32.TryParse(TryCast(value, String), intValue) AndAlso intValue = 0) Then
                rowItemsToRemove.Add(item)
            End If
        Next

        For Each rowItem As UltraDataRow In rowItemsToRemove
            ultraDataSource1.Rows.Remove(rowItem, True)
        Next
    End Sub


    I hope this helps.

    Best regards,
    Dimi
    Developer Support Engineer
    Infragistics Inc

  • 12773
    posted

    Hello Kevin,

    I reproduce your problem , I get the same behavior when I go over the UltraDataSource rows and delete some of them, if I rise the Delete Row Event and delete the same row form the external data using this row: Me.externalData.Rows.RemoveAt(e.Row.Index)

    The reson for that is very simple. You receve the same index, that’s alredy removed from the UltraDataSource and try to remove it from the external Data, that’s’ fine. First time when the event is fired, this is fine for both collections. But for the second you receive the index and try to remove it from the same location form the externalData  collection, but there is already removed record, so this index is incorrect, that’s way you are able to remove only even records.

    One way to avoid this behavior when you are going to trough a collection an remove items from it, and update other related collection. You need to collect the removed records and remove them at once if you are using code for doing that, and without fired the event for delete.

    I hope this helps.

    Best regards,
    Dimi
    Developer Support Engineer
    Infragistics Inc.