Solved

logical help needed on sample code - link supplied - dataview.count doesn't equal 0 although it should

Posted on 2004-08-31
15
204 Views
Last Modified: 2010-04-23
hi, i have an sample application which adds and removes items from a dataview.
each row has a description of the item, plus a qty value.
this is the logic.

the user can select up to 3 items from the available list of items. (there are only 3 available options)
when the user selects a item, the item will be listed on his order. the user can select multiple items at once.
if the user selects for e.g. 3 items, all 3 items will be listed on his order.
if the user again selects a item that is already on the list, then the program will only add 1 to the quantity field of the item.

'for each item that has been selected
 For i = 0 To dvStock.Count - 1

                Dim MyString As String = dvStock.Item(i)("KeyCode")
                'check if user has already have the key in the dataview.
                dv.RowFilter = "KeyCode= " & MyString.ToString & ""
'now if the item is not in the dataview yet, then dv.count should be 0
                Try
                    'if the user already has key X in the dataview, just add 1 to the qty
                    If dv.Count > 0 Then          'if there are already keys of this kind in the dataview, add 1 to the qty
                        dv(0)("KeyQty") += 1
                        dv.RowFilter = Nothing
                        'else add the item as a new item
                    ElseIf dv.Count = Nothing Then            'if there are no keys of this kind in the  dataview, add the key
                        dv.RowFilter = Nothing
                        Dim NewKey As DataRowView = dv.AddNew
                         NewKey("Description") = dvStock.Item(i)("Description").ToString
                        End If
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            Next

now the problem is when i select 3 items at once, then it returns dv.count = 1 after the first loop, and i just add the qty to the field the whole time , while i should be adding a new item, and the dv.count should be zero.
thus the first round executes fine, and the item is added to the dataview, but the second time around, the item is not added, because the count does not equal zero, (it is 1) although the filter i do should return 0, because there is no key of that kind in the dataview yet.
if you'd like to have a look at the sample. pls go to
URL: http://home.ripway.com/2004-8/159283/EEDataViewUpdate2.zip
the plan is to load the program, wait a few seconds , then click on the 5th column and a hourglass will appear.
click on it, and then select products of the list by using the checkbox, press return.
thanks!

0
Comment
Question by:jxharding
  • 11
  • 4
15 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11939964
Hi jxharding, I think it has something to do with the fact that the added rows haven't been accepted by the dataset yet.

obviously .acceptchanges is out of the question. But maybe you can play around with the
rowstatefilter

try adding this before the rowfilter

dv.RowStateFilter = DataViewRowState.ModifiedCurrent
dv.RowFilter = "KeyCode= '" & MyString.ToString & "'"

0
 

Author Comment

by:jxharding
ID: 11940175
hi ronald, i think thats what i need to proceed, it doesnt show right yet, do you think i should just play around with the different options for rowstatefilter?
is there any way to concatenate different rowstatefilter options e.g. to use modfiedcurrent + modifiedoriginal?

0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11940185
Hi jxharding,

I'll try if I can get the right combination, what didn't work with the modifiedcurrent, I tried that and that seemed to work ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jxharding
ID: 11940239
if i for e.g. click on add row , and then select 2 items, the first one will correctly add, but the second one will still have a rowcount of 1, thus adding 1 to the qty.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11940243
to use different options use

                dv.RowStateFilter = DataViewRowState.ModifiedCurrent & DataViewRowState.ModifiedOriginal
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11940261
oeps, that doesn't seem to work sorry, I'll keep looking
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11940310
to use a combination use

dv.RowStateFilter = DataViewRowState.ModifiedCurrent or DataViewRowState.ModifiedOriginal
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11940333
Hi Jxharding,

Bad news, the dataview rowfilter cannot be changed if there are changes pending in the filter, so we have to look at another way to do this. (you can test this by adding an acceptchanges to your dataset then it works perfectly, bugger!)
0
 

Author Comment

by:jxharding
ID: 11940365
hi ronald, whoa thats a downer! :)
in other words i cant use any rowstatefilter as well till i acceptchanges?
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11940406
no the rowstatefilter will work the rowfilter won't
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11940416
Give me 15 minutes and I'll look for another solution. (maybe 20 minutes is better ;-))
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 11940447
This seems to work

   Private Sub FillfrmListStock()

        If cnKeyGen.State = ConnectionState.Closed Then
            cnKeyGen.Open()
        End If

        DsMasterKeyDataCN.Clear()
        daMasterKeyData.Fill(DsMasterKeyDataCN)

        'load a new form so the user can select a key
        Dim frmFill As New frmListStock
        frmFill.SelectStockForMainForm = True

        Dim dvFillStock As DataView
        dvFillStock = New DataView(DsMasterKeyDataCN.Tables("MasterKeyData"))
        frmFill.MyDataView = dvFillStock
        cnKeyGen.Close()

        If frmFill.ShowDialog = DialogResult.OK Then
            'only select the rows that are checked
            dvFillStock.RowFilter = "CHECKBOX1 = TRUE"
            Dim i As Integer

            'if the last character is "0" delete it
            Dim k As Integer = dv.Count - 1
            If dv(k)("KeySeries").ToString = "" Then
                dv(k).Delete()
            End If

            'for each one selected, check if it is already in the current dataview
            For i = 0 To dvFillStock.Count - 1

                Dim MyString As String = dvFillStock.Item(i)("KeySeries")
                'check if user has already have the key in the dataview.
                'Dim t As Integer = dv.Count
                '                dv.RowFilter = Nothing
                dv.Sort = "KeySeries"
                Dim frow As Integer = dv.Find(MyString.ToString)
                'dv.RowFilter = "KeySeries = '" & MyString.ToString & "'"
                'Dim t2 As Integer = dv.Count
                Try
                    'if the user already has key X in the dataview, just add 1 to the qty
                    If frow > -1 Then
                        dv(frow)("KeyQty") += 1
                        'dv.RowFilter = Nothing
                        'else add the item as a new item
                    ElseIf frow = -1 Then
                        '                        dv.RowFilter = Nothing
                        dv.AllowNew = True
                        Dim NewKey As DataRowView = dv.AddNew
                        NewKey("KeySeries") = dvFillStock.Item(i)("KeySeries").ToString
                        NewKey("Description") = dvFillStock.Item(i)("Description").ToString
                        NewKey("KeyCodeID") = dvFillStock.Item(i)("KeyCodeID").ToString
                        NewKey("JobID") = Me.SelectedJob.JobID
                        dv.AllowNew = False
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            Next
        End If
    End Sub
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11940449
and only in 7 minutes :-)
0
 

Author Comment

by:jxharding
ID: 11940508
hi ronald,
i honestly dont think i would have been able to pull that off myself, ive been battling with this for at least a good 6 hours
and i learn't as well, e.g. i didnt know of dv.find and that it returns the row
thanks a million!!!
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11940528
Glad I could help Jxharding, see you in a next thread.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - Windows Protected Your PC 7 52
How do sunrise and sunset times change with altitude 14 46
Passing data between Forms 3 26
Web page design problem 3 14
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question