?
Solved

index Out OF Range Exception error

Posted on 2016-11-21
4
Medium Priority
?
96 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 1000 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 1000 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

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans
Suggested Courses

765 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