Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

Get first date and last date from datatable.

Hi,

I have a datatable with transaction date on it, let's say TrsDate, and other data. The data is not sorted by TrsDate.
How to get the first transaction date and last transaction date from the datatable?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of emi_sastra
emi_sastra

ASKER

Hi Dhaest,

Could I use :

dim dteMinDate as date =datatable.Compute("Min(TrsDate)", Nothing)
dim dteMaxDate as date =datatable.Compute("Max(TrsDate)", Nothing)

Thank you.
Hi

is the TrsDate attribute type of date ? then sort the table and then select the first and last rows date

Dim sortstr As String = "TrsDate,otherColumn,otherColumn DESC"
YourDataTable.DefaultView.Sort = sortstr
Return YourDataTable

vbturbo
Hi Dhaest,

I've tried it. It works.

Thank you very much for your help.

Hi VBTurbo,

I am sorry, Dhaest has solved my problem.

Thank you.
No problem at all.

I didn't see Dhaest post wich solves your question in many ways.
and then i would not even have posted my own comment

/vbturbo
The hints (not exact command) provided by Dhaest obviously solve my problem.

1. use the DataTable.Compute function.

maximum:  datatable.Compute("Max(myColumn)", Nothing)
minimum: datatable.Compute("Min(myColumn)", Nothing)

The second parameter is a filter, incase you want to limit the rows that are
applied to the aggregate function.

2. Create a dataview and order it
dv as new dataview(datatable)
dv.sort="StaffIdColumn, DESC"
Minimum --> dv(dv.count-1)("mycolumn")
Maximum --> dv(0)("mycolumn")

Thank you.