Click here to Skip to main content
14,333,447 members
Rate this:
Please Sign up or sign in to vote.
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

private void dgrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
   if(e.RowIndex>=0)
   {
     Tot=0;
     Double FinVal=CalculateTotal();
     label1.Text=FinVal.Tostring();
   }     
}
Double Tot=0;
Private Double CalcualteTotal()
{
     for(int i=0;i< dgrid.Rows.Count;i++)
     {
     double V = string.IsNullOrEmpty(dgrid.Rows[i].Cells["Service"].Value.ToString()) ? 0 :Convert.ToDouble(dgrid.Rows[i].Cells["Service"].Value.ToString().Replace('$',' ').Trim());
Tot+=V;
     }
     return Tot;
}
   
v2
Comments
lucasgrohl 1-Nov-10 0:37am
   
When I execute the application with this code it shows me the message: "Object reference not set to an instance of an object."

Any idea of what it can be?

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. Can it interfere in anyway?
Pawan Kiran 1-Nov-10 5:49am
   
Enter any Value in the Cell, and put Debugger in Cell_ValueChanged Event.
and Trace out the Value in Immediate Window(Ctrl+Alt+I)..
Value you are Getting With $ or Without $. and Do According to the Value you are Getting.
Pawan Kiran 1-Nov-10 8:09am
   
Take a look at my updated Answer.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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 &gt; 0)
{

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

MyConnection.Close();
Use it
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

http://aspalliance.com/782

Follow this kink u will get the code of ur problem
   
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. =/
Rate this:
Please Sign up or sign in to vote.

Solution 5

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;
}
}

}
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100