Jacque Scott
asked on
Help with understanding this code and rewriting code
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.
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
Doc3.docx
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();
}
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))
Doc3.docx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It loos like it's not accounting for null values.
How about something like:
or
I'm not a LINQ or C# person, so my syntax might be off a bit.
How about something like:
3: select Convert.ToDecimal(Rows.Cells[AmountColumnName].Value = null : 0 : Rows.Cells[AmountColumnName].Value)
or
2: where Convert.ToBoolean(Rows.Cells[CheckedColumn].Value) == true && Rows.Cells[AmountColumnName].Value != null
I'm not a LINQ or C# person, so my syntax might be off a bit.
ASKER
public static decimal GetCheckedSum(this DataGridView sender, string ColumnName, string AmountColumnName)
{
return
(
from Rows in sender.Rows.Cast<DataGridV
where Convert.ToBoolean(Rows.Cel
&& Convert.ToString(Rows.Cell
select Convert.ToDecimal(Rows.Cel
).Sum();
}