Solved

Help with understanding this code and rewriting code

Posted on 2016-08-09
3
63 Views
Last Modified: 2016-08-09
I found this code and I am able to use it in another program with no problem.  It gets me the total of the line items checked.

Now I need to change it but I haven't seen code written like this. Can someone help me read it?  I get the basics but I need help adding to it.

public static decimal GetCheckedSum(this DataGridView sender, string CheckedColumn, string AmountColumnName)
        {
            return
                (
                    from Rows in sender.Rows.Cast<DataGridViewRow>()
                    where Convert.ToBoolean(Rows.Cells[CheckedColumn].Value) == true
                    select Convert.ToDecimal(Rows.Cells[AmountColumnName].Value)
                ).Sum();
        }

Open in new window


As you can see below if there is a BillAmt then there is nothing in the PaymentAmt Column.  When I run the above code I get an error because there is no number in the PaymentAmt column.

I have a grid that displays the amt of a bill or a payment.  If the user clicks on the checkbox I want to get the Total for the Bills and Payments.  Here is how I have the grid laid out.  If it is all messy I have attached a screen print to see.

CheckBox         ID                   BillAmt            PaymentAmt
                           45                    4,500.00                      
                           46                       200.00    
                           47                                               4,000.00
                           48                    2,000.00
                           49                                               5,000.00


I would like to add an IF Statement or something similar
 if (BillAmt == "")
{
    BillAmt = 0;
}

OR in the SELECT portion change it to this

select ISNULL(Convert.ToDecimal(Rows.Cells[AmountColumnName].Value), 0, Convert.ToDecimal(Rows.Cells[AmountColumnName].Value))

Open in new window



Doc3.docx
0
Comment
Question by:huerita37
3 Comments
 
LVL 42

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41749214
Given your event method:
public static decimal GetCheckedSum(this DataGridView sender, string CheckedColumn, string AmountColumnName)
        {
            return
                (
                    from Rows in sender.Rows.Cast<DataGridViewRow>()
                    where Convert.ToBoolean(Rows.Cells[CheckedColumn].Value) == true
                    select Convert.ToDecimal(Rows.Cells[AmountColumnName].Value)
                ).Sum();
        }

Open in new window


This method returns a decimal value based on the results of the Linq query:

from Rows in sender.Rows.Cast<DataGridViewRow>()
where Convert.ToBoolean(Rows.Cells[CheckedColumn].Value) == true
select Convert.ToDecimal(Rows.Cells[AmountColumnName].Value)

Open in new window


That Linq query is looking at the rows in the gridview, where the checkbox has been selected, and then summing the value of the amount column for those rows.

The amount column is dependent on what's passed to the method.

This doesn't answer your entire question.  But hopefully it explains the method and how it's working.
1
 

Author Closing Comment

by:huerita37
ID: 41749270
Thank you very much.  You pointed me in the right direction.  This is what I ended up with and is  working:

public static decimal GetCheckedSum(this DataGridView sender, string ColumnName, string AmountColumnName)
        {
            return
                (
                    from Rows in sender.Rows.Cast<DataGridViewRow>()
                    where Convert.ToBoolean(Rows.Cells[ColumnName].Value) == true
                    && Convert.ToString(Rows.Cells[AmountColumnName].Value) != ""
                    select Convert.ToDecimal(Rows.Cells[AmountColumnName].Value)
                ).Sum();
        }
0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41749285
It loos like it's not accounting for null values.

How about something like:
3: select Convert.ToDecimal(Rows.Cells[AmountColumnName].Value = null : 0 : Rows.Cells[AmountColumnName].Value)

Open in new window


or
2: where Convert.ToBoolean(Rows.Cells[CheckedColumn].Value) == true && Rows.Cells[AmountColumnName].Value != null

Open in new window


I'm not  a LINQ or C# person, so my syntax might be off a bit.
1

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Single Form 8 28
SQL Exceptions 3 39
Coding C# in Linux 8 33
I need help converting a bitmap to an image in VB.Net 1 14
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 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

16 Experts available now in Live!

Get 1:1 Help Now