Solved

index Out OF Range Exception error

Posted on 2016-11-21
4
91 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
Introduction to Processes
Starting up a Project

696 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