Solved

DataTable calculated field question

Posted on 2014-11-06
2
99 Views
Last Modified: 2015-01-08
I have a datatable that has a grouping in it and I want to be able to assign a value to a calculated field based on the first (or matching) fields in the group.

For example, if I have the following table:

MyGroup       MyDateVar
gr-1                2001-12-23
gr-1                2004-03-12
gr-1                2014-10-04
gr-1                2003-12-23
gr-1                2012-03-12
gr-1                2000-10-04

I would like to have a calculated column that would have a value (say 10) for the oldest MyDateVar for the group.  The final result would be:


MyGroup       MyDateVar      CalcField
gr-1                2001-12-23      0
gr-1                2004-03-12      0
gr-1                2014-10-04      10
gr-1                2003-12-23      0
gr-1                2012-03-12      10
gr-1                2000-10-04      0

Can this be done with a datacolumn calculated field?
0
Comment
Question by:hotshottools
2 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 40426157
No

Surely possible by either the underlying query (server side) or using some LINQ (client side) ..
0
 
LVL 11

Accepted Solution

by:
louisfr earned 250 total points
ID: 40426896
To do that, you need an auxilliary table and a relation between your table (child) and the auxilliary (parent).

Parent table contains columns MyGroup, OldestDate
Child table contains columns MyGroup, MyDateVar, CalcField
Set OldestDate's Expression property to "Min(Child.MyDateVar)"
Set CalcField's Expression property to "iif(MyDateVar=Parent.OldestDate, 10, 0)"

Only constraint: you have to add a row in the parent table for each group. Nothing impossible to make automatic.

Here's a bit of code I typed to test all this.
class CT : DataTable
{
    public event DataTableNewRowEventHandler RowAdded;
    public void AddRow(params object[] data)
    {
        var row = NewRow();
        row.ItemArray = data;
        var ev = RowAdded;
        if (ev != null)
        {
            ev(this, new DataTableNewRowEventArgs(row));
        }
        Rows.Add(row);
    }
}

static void Main()
{
    DataTable parent = new DataTable
    {
        Columns = {
            "MyGroup",
            { "OldestDate", typeof(DateTime) }
        }
    };
    CT child = new CT
    {
        Columns = {
            "MyGroup",
            { "MyDateVar", typeof(DateTime) },
            { "CalcField", typeof(int) }
        }
    };
    parent.PrimaryKey = new[] { parent.Columns["MyGroup"] };
    DataSet ds = new DataSet
    {
        Tables = { parent, child },
        Relations = { new DataRelation("r", parent.Columns["MyGroup"], child.Columns["MyGroup"]) }
    };
    parent.Columns["OldestDate"].Expression = "Min(Child.MyDateVar)";
    child.Columns["CalcField"].Expression = "iif(MyDateVar=Parent.OldestDate,10,0)";
    child.RowAdded += (o, e) => {
        if (!parent.Rows.Contains(e.Row["MyGroup"]))
            parent.Rows.Add(e.Row["MyGroup"]);
    };
    child.AddRow("gr1", new DateTime(2014, 10, 1));
    child.AddRow("gr1", new DateTime(2014, 10, 2));
    child.AddRow("gr1", new DateTime(2014, 10, 1));
    child.AddRow("gr1", new DateTime(2014, 1, 1));
}

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now