Solved

Adding a row to Datatable - urgent help needed

Posted on 2010-11-21
4
307 Views
Last Modified: 2013-12-17
Hi,
I have a datatable with 2 columns "LONG" and "SHORT". before binding to grid, i need to add one more column "NET" .
NET = LONG - SHORT.
I need to calculate NET value by substracting short from long and I have to handle NULL values too if in case short or long is null.

Finally, my datatable shd be

long      short     Net
2              1          1
3               3          0

Please someone help me... I tried dng this bt I am missing something. i am not getting exaclty the way i am looking for. Please help me...

Thanks...

0
Comment
Question by:gautam_reddyc
  • 3
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
Should be easy enough. Try:

YourDataTable.Columns.Add(new DataColumn("Net", typeof(int), "long - short"));

Open in new window

0
 

Author Comment

by:gautam_reddyc
Comment Utility
Hi carl tawn,

This will work if you have only one row. What if you have mutliple rows of long and short... If we do foreach for all rows, only first time we can add column. In the next statement we will get an error saying already column  exists.

Please help me how to do this...
0
 

Author Comment

by:gautam_reddyc
Comment Utility
sorry, my previous comment was wrong...
my question is: If "LONG" or "Short" values are NULL in database, the datatable is coming with empty row. In that case it's not working. In other words,
for ex: Long value is 2 and short is NULL in databse.. so in datatbale it is empty.. In that case NET is also coming as empty instead of 2 which is Long value... How to handle NULL values.. Please help me...
0
 

Author Closing Comment

by:gautam_reddyc
Comment Utility
thanks... it worked.. I am explicitly handling the NULL values...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now