Solved

VB.Net - For Loop Error

Posted on 2016-11-19
5
31 Views
Last Modified: 2016-11-28
I am getting an error to look at my datagridview and if one of the column is empty to highlight the entire row.  It is working but spitting an error at the end of the loop.

Here's my For loop to check the value.
 For Each row As DataGridViewRow In dgDetail.Rows
                Dim service As String = row.Cells(4).Value.ToString
                If service = Nothing Then
                    row.DefaultCellStyle.BackColor = Color.Red
                End If
            Next

Open in new window


At the end of the loop I get the following error:
ForLoopError
0
Comment
Question by:holemania
  • 2
  • 2
5 Comments
 
LVL 24

Expert Comment

by:Pawan Kumar
ID: 41894485
Control must be going through empty row of dgDetail or cell value is nothing.

<<Put a debug point after the loop, run the program>>
Debugging - Can you place your mouse on the dgDetail and check the values after the exception.

Hope it helps !
0
 

Author Comment

by:holemania
ID: 41894497
I did debug and it is going through empty row.  Shouldn't it exit the loop if no rows left?
0
 
LVL 24

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 100 total points
ID: 41894503
We have to remove the empty row using below code-

In Form1_Load put this line-
dgDetail.AllowUserToAddRows = False

Or  you can do like below-

dgDetail.Rows - 1

For Each row As DataGridViewRow In dgDetail.Rows - 1 
                Dim service As String = row.Cells(4).Value.ToString
                If service = Nothing Then
                    row.DefaultCellStyle.BackColor = Color.Red
                End If
            Next

Open in new window




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.

0
 
LVL 27

Accepted Solution

by:
Ark earned 400 total points
ID: 41895172
this is because for some row.Cells(4).Value is nothing, so it can not be converted to string. Usualy it happens with new row, so you can use
If row.IsNewRow Then Continue For

Open in new window

Or you can check value instead of string:
If row.Cells(4).Value Is Nothing then row.DefaultCellStyle.BackColor = Color.Red 

Open in new window

0
 

Author Closing Comment

by:holemania
ID: 41904890
Thank you.  Went with Ark's solution since that worked.  Also thanks to Pawan for contributing.
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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

17 Experts available now in Live!

Get 1:1 Help Now