Solved

VB.Net - For Loop Error

Posted on 2016-11-19
5
51 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
[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
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 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