Solved

index Out OF Range Exception error

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

911 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

19 Experts available now in Live!

Get 1:1 Help Now