Solved

index Out OF Range Exception error

Posted on 2016-11-21
4
33 Views
Last Modified: 2016-11-21
I am quite a newbie in C# and I am unable to find what's wrong in this code and to fix it:
The purpose is to detect each new line added programmatically to the dgv and work on it.

private void ExtractLeftArmValue()
        {
            String searchValue = "337";
            int linefeeded = 1;
            int row = -0;
            string rowstring = row.ToString();
            string linestring = linefeeded.ToString();
           
            for (row = -1; row < dgvvNodes.Rows.Count ; row++)
            {   
                if (dgvvNodes.Rows[row].Cells[0].Value.ToString().Equals(searchValue))   // error comes from that line
                {    
                    string testdata = dgvvNodes.Rows[row].Cells[9].Value.ToString();
                    lbxElbowAngle.Items.Add(testdata);
                    linefeeded = linefeeded++;
                } 
            }
        }

Open in new window

I guess it is quite obvious but i've spent too many hours on that !
Can an expert help me ?
0
Comment
Question by:Paulagier
  • 2
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41895526
change
for (row = -1; row < dgvvNodes.Rows.Count ; row++)
to
for (row = 0; row < dgvvNodes.Rows.Count ; row++)

a row with index -1 won't exist
0
 
LVL 18

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 250 total points
ID: 41895542
Control must be going through empty row of dgvvNodes.Basically you will always have an empty row at the end of the DataGridView. It helps the end user to add a new row. So we can say it is by design.            


OPTION 1 -

In Form1_Load put this line-
dgvvNodes.AllowUserToAddRows = False
            

OPTION 2 -

private void ExtractLeftArmValue()
        {
            String searchValue = "337";
            int linefeeded = 1;
            int row = 0;
            string rowstring = row.ToString();
            string linestring = linefeeded.ToString();
           
            for (row = 0; row < dgvvNodes.Rows.Count - 1 ; row++)
            {   
                if (dgvvNodes.Rows[row].Cells[0].Value.ToString().Equals(searchValue))   // error comes from that line
                {    
                    string testdata = dgvvNodes.Rows[row].Cells[9].Value.ToString();
                    lbxElbowAngle.Items.Add(testdata);
                    linefeeded = linefeeded++;
                } 
            }
        }

Open in new window

           

Hope it helps !!
0
 

Author Comment

by:Paulagier
ID: 41895574
The -1 index solves the out of rang exception but the loop does not work. I have only one line added thru the method "lbxElbowAngle.Items.Add(testdata);" whatever the number of lines added to the datagridview.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 250 total points
ID: 41895580
OK, your original question is solved.  Please close this and ask a new question about the new problem (but the loop does not work)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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