Solved

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

Posted on 2004-08-31
15
200 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now