Results 1 to 5 of 5

Thread: Dataview confusion

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    42

    Dataview confusion

    Hi All,

    I am new to VB.Net and I have noticed somthing strange with dataviews.
    The code below does not move through the dataview but displays only the results for the first record.

    VB Code:
    1. For i = 0 To dv.Count - 1
    2.             'dv(i).BeginEdit()
    3.             Console.WriteLine(dv(i)("Machine") & ": " & dv(i)("Display"))
    4.             'dv(i).EndEdit()
    5.         Next

    The code below shows all the details correctly.
    VB Code:
    1. Dim myDRV As DataRowView
    2.         For Each myDRV In dv
    3.             Console.WriteLine(myDRV("Machine") & ": " & myDRV("Display"))
    4.         Next

    As I said I am new to VB.Net so if someone could point out my mistake it would be appreciated.

    Regards,
    Adam.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Dataview confusion

    You must be doing something else wrong because they both work for me.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    42

    Re: Dataview confusion

    Thanks for replying, I am updating the rows on the first pass. When I move through the rows a second time it appears to get stuck on the first row.
    I have tried using the 'BeginEdit' & 'EndEdit' during the update but they do not appear to fix the problem.

    VB Code:
    1. dv(i)("ActualDisplayOrder") = i + 1
    2. etc....


    Sorry I did not mention this, I thought I might have had the basic code incorrect.

    Cheers,
    Adam.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Dataview confusion

    I think you need to show the full code.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    42

    Re: Dataview confusion

    Hi jmcilhinney,

    The code I have inherited is shown below. The issue is when the aShow(i) is being loaded. 'dv(i)("Display")' gives the array the correct value the first time the procedure is run but after that the loop appears to get stuck on the first record. It is quite interesting as when I use a 'dataRow' to enumerate through the dataview it gives the correct values. I have tried using 'BeginEdit' & 'EndEdit' but it does not correct it. Maybe it is because I am using a filter & sorting. The dataview is getting updated but it gets stuck on the first records as noted above.

    Any ideas would be appreciated ?


    VB Code:
    1. ' Class variable
    2.     Public dsMch As New DataSet
    3. Private Sub ChangeValue(ByVal blnUp As Boolean)
    4.  
    5.         Dim aRow As DataRow
    6.         Dim dv As DataView
    7.         Dim iMax As Integer
    8.         Dim i As Integer
    9.         Dim aShow() As Boolean
    10.         Dim blnCurrentDisplay As Boolean
    11.  
    12.         dv = New DataView(Me.dsMch.Tables(0))
    13.         dv.AllowEdit = True
    14.         dv.AllowDelete = False
    15.         dv.AllowNew = False
    16.  
    17.         dv.RowFilter = "Display = True OR Source = 'Schedule'"
    18.         dv.Sort = "ActualDisplayOrder ASC"
    19.  
    20.         ReDim aShow(dv.Count - 1)
    21.  
    22.         If blnUp Then
    23.             ' Increment ActualDispalyOrder
    24.             ' Set all DisplayOrders to 999
    25.             For i = 0 To dv.Count - 1
    26.                 'dv(i).BeginEdit()
    27.                 dv(i)("DisplayOrder") = 999
    28.                 Console.WriteLine(dv(i)("Machine") & ": " & dv(i)("Display"))
    29.                '**** Incorrect values when called more than once *****
    30.                 aShow(i) = dv(i)("Display")
    31.                 If i = dv.Count - 1 Then
    32.                     ' Set last value to zero
    33.                     dv(i)("ActualDisplayOrder") = 0
    34.                 Else
    35.                     ' Increment Actual display
    36.                     dv(i)("ActualDisplayOrder") = i + 1
    37.                 End If
    38.                 'dv(i).EndEdit()
    39.             Next
    40.         Else
    41.  
    42.         End If
    43.  
    44.         ' Sort by ActualDisplayOrder & set flag to show as
    45.         ' per array
    46.         'dv.Sort = vbNullString
    47.         dv.Sort = "ActualDisplayOrder ASC"
    48.         For i = 0 To dv.Count - 1
    49.             dv(i).BeginEdit()
    50.             dv(i)("Display") = aShow(i)
    51.             dv(i).EndEdit()
    52.         Next
    53.  
    54.         ' Update display order used to show machines
    55.         dv.RowFilter = vbNullString
    56.         dv.RowFilter = "Display = True AND Source = 'Schedule'"
    57.         dv.Sort = "ActualDisplayOrder ASC"
    58.         For i = 0 To dv.Count - 1
    59.             dv(i).BeginEdit()
    60.             dv(i)("DisplayOrder") = i
    61.             dv(i).EndEdit()
    62.         Next
    63. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width