Solved

index Out OF Range Exception error

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

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

749 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