1 Previous Next 

DataTable: Compute


This tip computes how many rows with dates 2 months before today in a datatable

This sample needs only a datagridview and a label on a form

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, _
     ByVal e As System.EventArgsHandles MyBase.Load
        Dim dt As New DataTable
        dt.Columns.Add("Count"GetType(System.Int32))
        dt.Columns.Add("Date"GetType(System.DateTime))
        For i As Integer = 0 To 5
            Dim dtime As DateTime = Now.AddMonths(-i + 3)
            dt.LoadDataRow(New Object() _
            {DateTime.DaysInMonth(dtime.Year, dtime.Month), dtime}, True)
        Next
        DataGridView1.DataSource = dt
        ' This is the sample
        Dim ThisDate As String = Now.ToString("#MM/dd/yy#")
        ThisDate = ThisDate.Replace("-""/")
        Label1.Text = dt.Compute("Sum(Count)""Date < " _
               & ThisDate).ToString
    End Sub
End Class



1 Previous Next