Solved

VB.Net - For Loop Error

Posted on 2016-11-19
5
23 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 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
Comment Utility
I did debug and it is going through empty row.  Shouldn't it exit the loop if no rows left?
0
 
LVL 17

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.  Went with Ark's solution since that worked.  Also thanks to Pawan for contributing.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

14 Experts available now in Live!

Get 1:1 Help Now