Solved

Sum of column in datagrid

Posted on 2006-06-11
5
646 Views
Last Modified: 2008-03-17
Hi, I want tot find the sum of a colum in a datagrid. Below is the code that I am using but the problemm is that if there is no data in the table, it does not work. I get the error message: Cast from type 'DBNull' to type 'Decimal' is not valid.


TotalSoldPrice = objDS.Tables(0).Compute("SUM(SoldBid)", String.Empty).ToString
txtTotalSoldPrice.Text = FormatCurrency(TotalSoldPrice)

Can you tell me how I can get around this. I want to use this code because it is the simplest one that I have found thus far.
Thank you.

P.S, I am using SQL database, SQL Server 2003 and APS.NET and VB.NET
0
Comment
Question by:Dorneika
[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
5 Comments
 
LVL 5

Accepted Solution

by:
t_itanium earned 63 total points
ID: 16883272
hi
instead of this....
TotalSoldPrice = objDS.Tables(0).Compute("SUM(SoldBid)", String.Empty).ToString
txtTotalSoldPrice.Text = FormatCurrency(TotalSoldPrice)

try to iterate ove all rows in the datagrid...  and check for the values..if it is not null...add them to avariable thenn display this variable....


algorithm:

for (each row in datagrid rows)
if (cell is not null)
variable+=cell.value


display variable

cheers
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 62 total points
ID: 16883562
Or if you want to do it from the datatable itself, you can add an expression column, and compute on that.  On these lines

        Dim dc As New DataColumn
        dc.Expression = "ISNULL(SoldBid,0)"
        dc.DataType = GetType(Decimal)
        dc.ColumnName = "SoldBidWithoutNull"
        objDS.Tables(0).Columns.Add(dc)
        TotalSoldPrice = objDS.Tables(0).Compute("SUM(SoldBidWithoutNull)", String.Empty).ToString

The creation of the new column would only need doing once, not every time you wanted to calculate/re-calculate the TotalSoldPrice.

Roger
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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