?
Solved

Calculated Columns, Datatable and Dataset Questions

Posted on 2007-10-04
9
Medium Priority
?
699 Views
Last Modified: 2013-11-07
Hello!
In Visual Studio 2003, I used the properties window of an untyped dataset object to create the dataset's tables, columns, etc...
Let's say that I created a table called Orders and that table has 3 columns: OrderID, UnitPrice and Quantity.
Now I want to add a calculated column to the table. The calculated column will be named TotalPrice and its Expression Property will be set to the value "UnitPrice * Quantity".
Now I add code to fill my custom dataset with data from the database and use it as a datasource for my datagrid.
Questions:
1) When I add a new row to the grid and insert values to the columns UnitPrice and Quanity (let's say $10 and 23), my TotalPrice column is not being calculated. I suppose that the calculation is done when the data is being retrieved from the database... Am I right?
2) How to make it calculate the TotalPrice when I type the values on the UnitPrice and Quantity columns at run-time?
3) Now suppose that I use the WriteXML method of the datase to save the dataset schema and data to an XML file... It is not saving the calculated column. Why?


1) Whe
0
Comment
Question by:andrepires
  • 4
  • 4
8 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20016420
To calculate it at run time, the best way is to use java script. on tab out of UnitPrice and Quantity text boxes or cells add a javascript function that multiplies these values and adds teh calcualted value in the totalPrice column
0
 
LVL 2

Author Comment

by:andrepires
ID: 20016446
Hi.
Iti is a windows forms application.
0
 
LVL 21

Accepted Solution

by:
surajguptha earned 1500 total points
ID: 20016510
ohh well if its a windows application, it becomes even easier. just trap the cellvalue_changed event of the datagrid and update the TotalPrice cell
0
Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

 
LVL 2

Author Comment

by:andrepires
ID: 20016561
ok.. this answer is for the question 1 and 2.
What about the question 3?
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 20016575
Does your datatable that is fetched from the database have this TotalPrice column?
0
 
LVL 2

Author Comment

by:andrepires
ID: 20016660
No sir...
It does no matter since I'm talking about a disconnected scenario using ADO.Net and XML.
The fact is that the WriteXML method of the dataset should save any element, such as tables, columns and data to the target XML file. It is including the TotalPrice element definition in the schema, but is not saving the calculation value...
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 20016675
When the cell is updated, iterate through the DataTable that you are using it to bind to the datagrid, update the datatable too.
0
 
LVL 2

Author Comment

by:andrepires
ID: 20018216
Ok.. it seams like it is a bug of VS 2003 and/or Datagrid.
I reproduced the same code using VS 2005 and everything is perfect.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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 …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

571 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