Solved

VB.Net - For Loop Error

Posted on 2016-11-19
5
41 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 28

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 28

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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