Populating a data grid with a calculated field...

  • Thread starter Thread starter Unforgiven
  • Start date Start date
U

Unforgiven

Hello All,

I have the following query...

SELECT Demographics.Full_Name,
Demographics.Year_of_birth,
Status.Status_OK,
SUM(2004 - Demographics.Year_of_birth) AS Age
FROM Demographics
INNER JOIN Status ON Demographics.PrimaryKey = Status.PrimaryKey
GROUP BY Demographics.Full_Name, Demographics.Year_of_birth,
Status.Status_OK

I also have a grid on VB ASP.NET web form which is boound to this query that
has 3 columns
Full Name, Year of Birth and Status

How can I create a 4th column in the data grid to display the age that is
returned by the query in VS.NET 2003?

I'm new to .NET

Thanks,

-U
 
Hi
you can build your datatable object , add your four columns to it then
bind it to the datagrid.
You can build your datatable as follows :

Dim parent As new DataTable("Parent");
parent.Columns.Add(new DataColumn("name", typeof(string)));
parent.Columns.Add(new DataColumn("year_of_birth", typeof(decimal)));
parent.Columns.Add(new DataColumn("status", typeof(string)));
parent.Columns.Add(new DataColumn("age", typeof(decimal)));

parent.PrimaryKey = new DataColumn[] { parent.Columns["name"] };



bound the three first columns Then for the forth columns that will be
calculated add the appropriate expression that would return the calculated
age
parent.Columns["age"].Expression =
"you may but your query ";

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
Mohamoss & unforgiven,
typeof is a C# operator, in VB.NET you need to use the GetType operator (not
to be confused with the Object.GetType & Type.GetType methods). Also [] is
C#:

Dim parent As New DataTable("Parent")
parent.Columns.Add(New DataColumn("name", GetType(string)))
parent.Columns.Add(New DataColumn("year_of_birth", GetType(decimal)))
parent.Columns.Add(New DataColumn("status", GetType(string)))
parent.Columns.Add(New DataColumn("age", GetType(decimal)))

parent.PrimaryKey = New DataColumn() { parent.Columns("name") }

I normally include the expression on the New DataColumn constructor, rather
then set Expression explicitly, either works.

parent.Columns.Add(new DataColumn("age", GetType(decimal), "the
expression"))

For information on expressions in the DataSet object model see the
DataColumn.Expression help topic.

http://msdn.microsoft.com/library/d...fsystemdatadatacolumnclassexpressiontopic.asp

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top