Solved

DataTable calculated field question

Posted on 2014-11-06
2
105 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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…

777 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