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
Solved

VB.Net - For Loop Error

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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…

828 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