foreach not doing last item

Posted on 2011-10-04
Last Modified: 2013-12-16
This is my foreach statement:            

foreach (DataGridViewRow dataRow in dgv1.Rows)  
                int rowNbr = dataRow.Index;

Open in new window

There are 23 rows in the DGV... it stops after DataRow.Index = 22 (code for index 22 gets executed, 23 does NOT)

Any ideas why?
Question by:rmmarsh
    LVL 6

    Accepted Solution

    The index starts with 0. So if you have 23 rows, the index runs from 0 to 22.

    Author Comment

    Hmmm... let me cogitate on this for a bit...

    Author Closing Comment

    Got it!  

    Pilot error on my part... but your comment helped immensely...  thank you
    LVL 6

    Expert Comment

    You're welcome.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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.

    Suggested Solutions

    Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 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

    12 Experts available now in Live!

    Get 1:1 Help Now