Solved

Sum of column in datagrid

Posted on 2006-06-11
5
636 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
5 Comments
 
LVL 5

Accepted Solution

by:
t_itanium earned 63 total points
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

9 Experts available now in Live!

Get 1:1 Help Now