Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DataTable calculated field question

Posted on 2014-11-06
2
Medium Priority
?
109 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 750 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 750 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 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