Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a problem where I have a DataGridView with invoice information. One of the columns is the price of a service.

Right below this I have a label that needs to show the total value of this invoice, and it needs to be shown as the user types the values.

e.g.

Service | Value
Hotel | $200.00

Total: $200.00

After typing the next entry...

Service | Value
Hotel | $200.00
Taxi | $50.00

Total: $250.00

Also, I don't know how to use events. So, if you can explain me how to declare an event...

I know I must use method for this (e.g. private void dgrid_CellValueChanged(object sender, DataGridViewCellEventArgs e){ ... } ), but I don't know how to make this method to be called when there is a cell updating, in fact.


After this, when I try to use the solution proposed for Pawan, I got the following error:

"Object reference not set to an instance of an object."

On the line:

double V = string.IsNullOrEmpty(dgrid.Rows[i].Cells["Service"].Value.ToString()) ? 0 :Convert.ToDouble(dgrid.Rows[i].Cells["Service"].Value);


PS: I set this column as typeof(double) in the Form Load event and I am also using a column format as C2(currency). In this case, after entering the value, it puts the symbol $ in front of the value. Also, when I type something in one of the columns, a new line is automatically created.

Can someone help me? :confused:

Thanks in advance! ;)
Posted
Updated 31-Oct-10 20:20pm
v6
Comments
Dalek Dave 29-Oct-10 5:04am    
Edited for Grammar.

i put this code in the datagridview1_CellEnter event

if (e.ColumnIndex == 4)//4 is the column that you want to add up in real time
{
double sum = 0;
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if (!item.IsNewRow)
{
sum += Convert.ToDouble(item.Cells[4].Value.ToString());
label1.Text = "" + sum;
}
}

}
 
Share this answer
 
SqlDataAdapter adap = new SqlDataAdapter("select sum(Price)as Total from ASSSS where Invoice=1", MyConnection);
MyConnection.Open();
// DataSet ds = new DataSet();
DataTable dt = new DataTable();
adap.Fill(dt);

if (dt.Rows.Count > 0)
{

Label1.Text = dt.Rows[0]["Total"].ToString();
}

MyConnection.Close();
Use it
 
Share this answer
 
http://aspalliance.com/782

Follow this kink u will get the code of ur problem
 
Share this answer
 
Comments
lucasgrohl 29-Oct-10 7:15am    
The problem is that this is for when I already have the data stored. In my case, the user is typing the values and the sum of all the cells of this columns has to be shown in run time in a label. =/

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