Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to minus column Cost,insuranceCost and store the result in a column Total for each row?!

myCode:
ServiceInsuranceList = new System.Data.DataTable();

C#
private void BtnAdd_Click(object sender, EventArgs e)
       {

             GrdServiceInsurance.DataSource = ServiceInsuranceList;
                for (int i = 0; i < GrdServiceInsurance.Rows.Count; ++i)
                {
 GrdServiceInsurance.Rows[i].Cells["Total"].Value =(Convert.ToInt32( GrdServiceInsurance.Rows[i].Cells["Cost"].Value.ToString())) -(Convert.ToInt32( GrdServiceInsurance.Rows[i].Cells["InsuranceCost"].Value.ToString())) 
                }

       }


But it does not work...
error "Object reference not set to an instance of an object."
Posted
Comments
On which line?
je30ca 6-May-14 4:14am    
GrdServiceInsurance.Rows[i].Cells["Total"].Value =(Convert.ToInt32( GrdServiceInsurance.Rows[i].Cells["Cost"].Value.ToString())) -(Convert.ToInt32( GrdServiceInsurance.Rows[i].Cells["InsuranceCost"].Value.ToString()))
Okay do one thing now. While debugging in the immediate window, check the values for...

GrdServiceInsurance.Rows[i].Cells["Cost"].Value.ToString()

and

GrdServiceInsurance.Rows[i].Cells["InsuranceCost"].Value.ToString()

Do you see the correct values?

first you have to check whether cost and insurance cost is not null or blank. if one of the null or blank then error come. so null or blank convert into 0 from DB side.
 
Share this answer
 
 
Share this answer
 
i will suggest you to add one more column Total in your datatable itself and assign value using datatable iteration instead of gridview that will be fast & solve your problem.

In the above code GrdServiceInsurance.databind() is missing.
 
Share this answer
 
Comments
je30ca 6-May-14 5:38am    
how to sum two columns value in datatable?!
Manu Smriti 6-May-14 6:04am    
look at this you will get an idea, here in datatable we have column 'Total'.
I'm iterating thru' datatable , adding 2 in first column of datatable & storing it into Total Column
int sum;
int count=0;
foreach (var item in dtCombo.AsEnumerable())
{
sum = Convert.ToInt32(item[0]) + 2;
dtCombo.Rows[count++]["Total"] = sum;
}
Wrong place in your application to do the calculation. The code you show is part of the User Interface. Its purpose is displaying data and accepting user input. Don't do calculations here.

ServiceInsuranceList, if not totally ill-named, contains a list of objects. I don't know what objects, but they should be instances of a class that has properties for Cost and InsuranceCost.
Make this class have a property Total as well. Let it be read-only and let the class itself update Total whenever one of its dependencies (Cost and InsuranceCost) changes. Then let data binding do its magic.
 
Share this answer
 
Comments
je30ca 6-May-14 4:50am    
must be "total" a column of the table?!
now "Total" is just a column of DatagridView
lukeer 6-May-14 7:32am    
But the DataGridView is data-bound. IIRC, you can't fill individual cells in that case. Instead you have to provide all data via the given data source.

However, before we get to that point, sort out the problem at hand:
break up the calculation so you get integer values for Cost, InsuranceCost and Total, calculate, assign to the correct cell. Then you should see what step throws the exception (that's just another way to do what Tadit Dash suggested).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900