Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sum of column in datagrid

Posted on 2006-06-11
5
Medium Priority
?
655 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 252 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 248 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 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