|
-
Sep 3rd, 2010, 04:22 AM
#1
Thread Starter
Member
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
-
Sep 3rd, 2010, 05:39 AM
#2
Thread Starter
Member
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.
-
Sep 3rd, 2010, 06:20 AM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 3rd, 2010, 06:32 AM
#4
Thread Starter
Member
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
Last edited by PaulHarman; Sep 3rd, 2010 at 06:37 AM.
-
Sep 3rd, 2010, 07:06 AM
#5
Thread Starter
Member
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.
-
Sep 3rd, 2010, 07:11 AM
#6
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 3rd, 2010, 07:11 AM
#7
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
-
Sep 3rd, 2010, 07:33 AM
#8
Thread Starter
Member
Re: Is this the best way to do it?
arghhh I was close! I just put And, not AndAlso 
rep added guys
-
Sep 3rd, 2010, 07:54 AM
#9
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
This world is not my home. I'm just passing through.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|