Help > How to code a "count"...

todoink

Member
Joined
Jul 27, 2006
Messages
15
Programming Experience
Beginner
I'm here again...

This time I've already did a lot of readings and searchings but still I can't find the answer to my question..

Here's what I want to do...

I want to add a Label on my form. And on that Label, the total number of my records on my data grid will be shown...

It increases when I add a record and it decreases when I delete...

Anyone can help me with this one? It will be much appreciated. :)
 
You'll need a class level varaible..

VB.NET:
Private RowCounter as Integer = 0

When you fill the datatable..

VB.NET:
RowCounter = Dataadapter.Fill.....
 
Label.text = rowcounter.tostring

When you add a row..

VB.NET:
Rowcounter+=1
Label.text = Rowcounter.ToString

When you remove a row

VB.NET:
RowCounter-=1
Label.Text = rowcounter.tostring
 
Many many many thanks! vb.net forums have already contributed a lot on my project. I won't forget to include it on my protject's credits. :)
 
You also have the option of attaching event handlers to the datatable itself, whenever it changes, however it changes, the events will fire and you can update the label. That way you dont have to remember to update the counter everywhere it might possibly be updated.

vis, i'm curious.. why would you use a class wide variable at all? Why not just, every time the table is altered by a fill, add or delet, simply say:

Label1.Text = myDataTable.Rows.Count.ToString()

One less thing to forget to update..
 
(vis 781 commented that this may cause the entire collection to be counted each time - information from his university lecturer. this post details a crude investigation of the accuracy of the lecturer's claim)

Did microsoft really write their collections base such that accessing .Count iterates the entire collection?

i wrote the following code in a project I had open..

VB.NET:
        Debug.Print(DateTime.Now().ToString("hh:mm:ss.ffffff"))
        Debug.Print(TestDataSetInstance.BONDETAIL.Rows.Count.ToString())
        Debug.Print(DateTime.Now().ToString("hh:mm:ss.ffffff"))
        Debug.Print(TestDataSetInstance.BONDETAIL.Rows(0)(0).ToString())
        Debug.Print(DateTime.Now().ToString("hh:mm:ss.ffffff"))
 
 
 
        For i As Integer = 0 To 10000000
            If i Mod 10000 = 0 Then Debug.Print(DateTime.Now().ToString("ss.ffffff"))
            Try
                If TestDataSetInstance.BONDETAIL.Rows(i) Is Nothing Then
                    Exit For
                End If
            Catch ex As Exception
                Exit For
            End Try
 
        Next i
        Debug.Print(DateTime.Now().ToString("hh:mm:ss.ffffff"))

The table had 100,000 rows, the loop took 220 milliseconds to complete, accessing the Rows.Count and Rows(0)(0) each took 30 milliseconds.

Though crude and not very scientific, I would have expected the Rows.Count to take significantly longer than 30 milliseconds (same amount of time to access the first item in the list) if it was iterating the collection. Given that Rows, as a collection, has absolute control over the incomings and outgoings (Add/Range/Remove/Insert) it is very easy for the collection to maintain a count in the same way we do here.. Indeed all the list type structures we wrote at university had a constantly accurate counter of the length of the list - it comes in incredibly handy for e.g. a Linked List if you know that it contains 100 items, and the user asks for 101, you simply say:

if(userRequested > list.length-1) then "no deal"

rather than crawl to the end of the list, counting each as we go, only to find there is none.



So it appears that vis' university lecturer was possibly operating off old information or (if .net has always been this way) incorrect from the outset. The positive upshot is that todoink can use datatable.Rows.Count without fear of significant performance penalty, and this will reduce the coding necessary to count the rows.
 
Last edited:
Back
Top