For each loop help

compguru910

Well-known member
Joined
Nov 25, 2009
Messages
47
Programming Experience
3-5
Ok, Ive got a loop that is supposed to run through my datagridview and set the background color. I can get it to see the value in the cell, but it doesnt seem to run through the whole datagridview. Here is my code.

VB.NET:
    Public Sub ColorRows()

        For Each row As DataGridViewRow In dgvMain.Rows
            Select Case row.Cells(4).Value
                Case Is > Now
                    row.DefaultCellStyle.BackColor = Color.IndianRed
                Case Is = Now
                    row.DefaultCellStyle.BackColor = Color.CadetBlue
                Case Else
                    row.DefaultCellStyle.BackColor = Color.Green

            End Select
        Next

    End Sub

Thanks in advanced for the help
 
Do you have Option Strict On?
 
{Be hAppy}

Assuming that data gril view is showing dataes as string data type:

Try to replace line:

Select Case row.Cells(4).Value

as

Select Case cdate(row.Cells(4).Value)


Be sure that regional settings for Date is same in which data is presented in datagrid.
 
{Be hAappy}

'Alternate Way is
'Providing that you are using On Error Resume Next


Public Sub ColorRows()
dim n as integer=0

For Each row As DataGridViewRow In dgvMain.Rows
n+=1
try
Select Case row.Cells(4).Value
Case Is > Now
row.DefaultCellStyle.BackColor = Color.IndianRed
Case Is = Now
row.DefaultCellStyle.BackColor = Color.CadetBlue
Case Else
row.DefaultCellStyle.BackColor = Color.Green
catch
row.DefaultCellStyle.BackColor = Color.Green
end try

End Select
Next
'For be sure that look is running for all rows
msgbox n
msgbox dgvMain.Rows.count

End Sub
 
Thanks for the help. I believe the regional value is correct, because the if then statements are working fine without messing with it. The format though that the database is spitting back at me is yyyy-MM-dd, where as vb uses dd-MM-yyyy. It automatically converts it though when importing to the datagrid and any other date. I just have to convert it back when updating my tables. How do you implement the On Error Resum Next?
 
The point of asking about Option Strict On was to try and provoke the answer that send2te posted. Basically, if you are trying to compare two dates, then do so using the .NET way - Convert.ToDateTime or (better still) Date.TryParse. Your code should then look something like:-

VB.NET:
Public Sub ColorRows()
        For Each row As DataGridViewRow In dgvMain.Rows
            Select Case Convert.ToDateTime (row.Cells(4).Value)
                Case Is > Date.Now
                    row.DefaultCellStyle.BackColor = Color.IndianRed
                Case Is = Date.Now
                    row.DefaultCellStyle.BackColor = Color.CadetBlue
                Case Else
                    row.DefaultCellStyle.BackColor = Color.Green
            End Select
        Next
    End Sub

When I moved from VB6 to .NET, there were two things I learnt quickly from this forum:-

1. Option Strict On.
2. Remove the Microsoft.VisualBasic reference - removes all the old bad habits ;)

I also doubt that your dates are being 'converted' unless you specifically tell the system to convert them. They are probably just being 'displayed' differently.
 
Thanks for the help. I believe the regional value is correct, because the if then statements are working fine without messing with it. The format though that the database is spitting back at me is yyyy-MM-dd, where as vb uses dd-MM-yyyy. It automatically converts it though when importing to the datagrid and any other date. I just have to convert it back when updating my tables. How do you implement the On Error Resum Next?

{Be hAppy}

On Error Resume Next is a statement which provide surity that software will not break on any error raised and it will jump to next statement.

Always save data in database and grid view like "Date='" & Format(ADate,"dd-MMM-yy") & "'"
It will help you run away from regional settings problem.
 
Doesnt vb.net come default with option strict on? I was under the understanding that it did.

Thats almost exactly what my code looked like, except it was "Date.Now" it was just "Now" . Well, its works again, so thanks to you both.
 
Well, its works again, so thanks to you both.

:)

Always save data in database and grid view like "Date='" & Format(ADate,"dd-MMM-yy") & "'"
It will help you run away from regional settings problem.

The best way to avoid any regional settings problem is to save it in a date field, rather than convert it to a string.

My personal opinion of your "dd-MMM-yy" logic is flawed. I always prefer looking at dates in a yyyy-MMM-dd format, as the sorting is far more logical.
 
send2te said:
'Providing that you are using On Error Resume Next
On Error Statement (Visual Basic)
help said:
Whenever possible, we suggest you use structured exception handling in your code, rather than resorting to unstructured exception handling and the On Error statement.
Unstructured exception handling is a classic VB legacy. Exception handling may also be avoided if you validate the data before using it, for example Date.TryParse can be used to attempt conversion without throwing an exception. That said, it should not be necessary to convert db dates, if handled properly the date value returned from db should be type Date and not String. (though you may have to type cast sometimes)
compguru910 said:
Doesnt vb.net come default with option strict on? I was under the understanding that it did.
No, it doesn't. Perhaps it should? The loose type handling is one of charms of VB, but as the IDE and intellisense gets better it is easier also for beginners to have implicit code automatically corrected, which they perhaps could learn better habits from early on. I don't know which would be better. Type inference is also starting to play a role here.
 
Now is part of the runtime library, it returns Date.Now value. IMO getting used to the VB data types and .Net class library is better than sticking with legacy functionality that lack the OOP approach required with todays VB.Net development.
 
Back
Top