Solved

DataTable calculated field question

Posted on 2014-11-06
2
108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

624 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