Row count in DataView

Posted on 2011-10-21
Last Modified: 2012-05-12
I have a DataTable that is being load with rows from a database.

Once loaded I put the DataTable in a DataView and filter the results.

     Dim dv As DataView = dt.DefaultView
     dv.RowFilter = "id <> " & ScheduleID

I want to loop through the results BUT before I do that I want to first check if there are any rows to loop through.  Something like

If dv.Rows.Count > 0 Then
    ' Perform loop
End If.

How do I do this with a DataView?
Question by:cdemott33
    LVL 25

    Expert Comment

    by:Shaun Kline
    You can use dataview.Count.

    As similar option, if you do not necessarily need to perform logic if no rows are in the view is to use the FOR Each r as datarowview in dataview loop.

    Author Comment

    Are you saying that I don't need the DataView.Count check if I use the...

    For Each row as DataRowView In DataView
        ' Do stuff

    Please advise.
    LVL 25

    Expert Comment

    by:Shaun Kline
    The For Each is a means of looping through a collection of items, be that DataRowViews in a DataView or widgets in an IList. The loop will only occur when there are items in the collection.
    LVL 7

    Accepted Solution

    That is right.  You may want to do a null check though?  I'm not sure if the DataTable can ever have a DataView that is null..

    Dim dv As DataView = dt.DefaultView
    If dv IsNot Nothing Then
        For Each row as DataRowView In dv
            ' Do stuff
    End If

    Open in new window


    Author Closing Comment

    Thank you!

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    728 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

    16 Experts available now in Live!

    Get 1:1 Help Now