Is this the best way to do it?
Code:
Private Sub checkReturnedDate()
Dim rownumber As Integer = "0"
For Each row In DataGridView1.Rows
If Not DataGridView1.Item("Datereturned", rownumber).Value() = "" Then
Dim DateReturned As Date = DataGridView1.Item("Datereturned", rownumber).Value()
Dim twoweeksago As Date = Date.Today.AddDays(-14)
If DateReturned < twoweeksago Then
Console.Write(DateReturned)
End If
End If
rownumber = rownumber + 1
Next
End Sub
Basically, the field may be blank, or it is in the format 19-08-2010. I want to find any dates which are over 2 weeks from today. It seems to work ok, but just wanted to see if this was the best way to do it.
Thanks
Re: Is this the best way to do it?
Following on from this, I have a new form which has another datagridview with three columns (registration, linenumber (hidden) and a checkbox for moving from a greylist to blacklist).
My question is, how do I populate my second datagridview from the above code?
So...
My code above lists goes through the main form datagridview... any which rows with Datereturned which is older than 2 weeks ago, I need to add the "registration" and "linenumber" columns from the main form datagridview to my 2nd form datagridview.
Re: Is this the best way to do it?
try this:
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim items = From row In DataGridView1.Rows _
Let r = DirectCast(row, DataGridViewRow) _
Where CDate(r.Cells("dateReturned").Value) < Now.AddDays(-14) _
Select New Object() {r.Cells("lineNumber").Value, r.Cells("registration").Value}
For Each i In items
DataGridView2.Rows.Add(i)
Next
End Sub
Re: Is this the best way to do it?
Thanks dude, although it errors when theres a blank date
and i really dont understand how it works lol
Re: Is this the best way to do it?
The error I get is on the next line... Conversion from string "" to type 'Date' is not valid.
Re: Is this the best way to do it?
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'for each row in DataGridView1.Rows
'where it isn't a new row + the date in the dateReturned column is more than 14 days ago
'select a new object() array containg 2 elements - lineNumber + registration
Dim items = From row In DataGridView1.Rows _
Let r = DirectCast(row, DataGridViewRow) _
Where Not r.IsNewRow AndAlso r.Cells("dateReturned").Value <> "" AndAlso CDate(r.Cells("dateReturned").Value) < Now.AddDays(-14) _
Select New Object() {r.Cells("lineNumber").Value, r.Cells("registration").Value}
'for each array in items, add a new row to DataGridView2
For Each i In items
DataGridView2.Rows.Add(i)
Next
End Sub
End Class
Re: Is this the best way to do it?
Change the where clause to exclude blank values:
Code:
Where r.Cells("dateReturned").Value <> "" AndAlso CDate(r.Cells("dateReturned").Value) < Now.AddDays(-14)
-tg
Re: Is this the best way to do it?
arghhh I was close! I just put And, not AndAlso :(
rep added guys
Re: Is this the best way to do it?
In answer to post #1 you could consider using the following. Alternatively, can you add a hidden column to your data that is -1, 0 or 1 depending on the row's date? This might be better because then the time would be based on the server clock rather than the local PC clock. Not knowing what your application is, however, that may turn out to be bad advice...
Code:
If DateReturned.CompareTo(Today.AddDays(-14)) < 0 Then
Console.Write(DateReturned)
End If