A problem with datatable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Taftheman
    New Member
    • Nov 2006
    • 93

    #1

    A problem with datatable

    Hi, I am trying to do some data checking before commiting the data back to the database. I have a datagrid in this data grid i have textboxes. The user can edit what they like. The first row in the datagrid is a phantom row. This is so users can add a row into the grid. I have then used VB.NET to manually get the data from the grid into a datatable. What i need to do is check that the Start date of a record is after the end date of the previouse record. I thought this would be simple but i dosen't see to work.

    The code:

    I first order the datatable by the date

    datatable.defau ltview.sort = "Startdate DESC, EndDate DESC"

    Then I use a loop to check it out

    Dim rowcount as integer, _
    nextrow as integer,
    bDayGreaterThan 1 as boolean

    For rowcount 0 to datatable.rows. count - 1
    nextrow = rowcount + 1
    If not is dbnull(startdat e) then
    IF DateDiff(day, datatable.rows( rowcount).item( "StartDate" ), datatable.rows (nextrow).item( "EndDate")) then
    bDayGreaterThan 1 = True
    End if
    End if
    Next

    How ever this does not work, it sorts the datatable out ok but in the loop it doesn't start from the top. It seems to start from 0 which could be anywhere in the datatable any one have any clues

    Thanks
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    You have sorted the DefaultView, but are looping through the original DataTable.
    Try looping through the DefaultView.

    (psuedocode)
    For rowcount 0 to datatable.Defau ltView.Count - 1
    rowDate = datatable.Defau ltView(rowcount )("StartDate"). ..

    A "view" is just that: an in-memory representation of the original table but with a filter or sort applied to the rows. The original table still stays the same, unless you create a new table from the sorted view.

    Comment

    • madankarmukta
      Contributor
      • Apr 2008
      • 308

      #3
      Hi,

      use dataTable.Defau ltView.ToTable( ).Rows's row while you are in loop..
      because you are appplying filter to default view and not to the actual table.

      Thanks!

      Comment

      Working...