Results 1 to 9 of 9

Thread: Is this the best way to do it?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    54

    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

  2. #2

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    54

    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.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Is this the best way to do it?

    try this:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim items = From row In DataGridView1.Rows _
    3.                 Let r = DirectCast(row, DataGridViewRow) _
    4.                 Where CDate(r.Cells("dateReturned").Value) < Now.AddDays(-14) _
    5.                 Select New Object() {r.Cells("lineNumber").Value, r.Cells("registration").Value}
    6.  
    7.     For Each i In items
    8.         DataGridView2.Rows.Add(i)
    9.     Next
    10. End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    54

    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    54

    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.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Is this the best way to do it?

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         'for each row in DataGridView1.Rows
    5.         'where it isn't a new row + the date in the dateReturned column is more than 14 days ago
    6.         'select a new object() array containg 2 elements - lineNumber + registration
    7.         Dim items = From row In DataGridView1.Rows _
    8.                     Let r = DirectCast(row, DataGridViewRow) _
    9.                     Where Not r.IsNewRow AndAlso r.Cells("dateReturned").Value <> "" AndAlso CDate(r.Cells("dateReturned").Value) < Now.AddDays(-14) _
    10.                     Select New Object() {r.Cells("lineNumber").Value, r.Cells("registration").Value}
    11.  
    12.         'for each array in items, add a new row to DataGridView2
    13.         For Each i In items
    14.             DataGridView2.Rows.Add(i)
    15.         Next
    16.     End Sub
    17.  
    18. End Class

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    54

    Re: Is this the best way to do it?

    arghhh I was close! I just put And, not AndAlso

    rep added guys

  9. #9
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    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
  •  



Click Here to Expand Forum to Full Width