Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have dvg which is for category and make some calculations in every row like this [ Quantity * Price - Discount = Total ] and I want when I enter a value in every cell total Cell automatically changing to the new value I will attach codes I tried it works for show the total but when I move to the next row and start enter the values [ Quantity * Price - Discount ] all total values changed for the new value.

What I have tried:

C#
private void grid1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
           {
               if ( grid1.CurrentCell.ColumnIndex == 3)
               {
                   TextBox tb = (TextBox)e.Control;
                   tb.TextChanged += new EventHandler(tb_TextChanged);
               }

              if (grid1.CurrentCell.ColumnIndex == 5)
              {
                  TextBox tb5 = (TextBox)e.Control;
                  tb5.TextChanged += new EventHandler(tb5_TextChanged);
              }
              if (grid1.CurrentCell.ColumnIndex == 4)
              {
                  TextBox tb4 = (TextBox)e.Control;
                  tb4.TextChanged += new EventHandler(tb4_TextChanged);
              }
           }


tb5_TextChanged , tb4_TextChanged , tb3_TextChanged all same code 


C#
<pre>void tb5_TextChanged(object sender, EventArgs e)
        {
            var c5 = (sender as TextBox).Text;

            double No3 = 0;
            double No2 = 0;
            double No1 = 0;

            if (grid1.CurrentCell.ColumnIndex == 5)
                label8.Text = c5;
            
            if (grid1.CurrentCell.Value != null)
            {
                for (int i = 0; i < grid1.RowCount; i++)
                {
                    if (grid1.Rows[i].Cells[0].Value.ToString() != null && grid1.Rows[i].Cells[1].Value.ToString() != null)
                    {
                        if (Double.TryParse(label6.Text, out No2) && Double.TryParse(label7.Text, out No1) && Double.TryParse(label8.Text, out No3))
                            grid1.Rows[i].Cells[6].Value = (No1 * No2 - No3).ToString();
                    }
                }
            }
             
        }
Posted
Updated 22-Nov-22 19:35pm
v3
Comments
Richard MacCutchan 21-Nov-22 5:27am    
What do you mean by "did not work for me"?
kudim 21-Nov-22 5:30am    
For Ex : when I write 10 in Quantity Cell Total Cell value changed and now it is 10 but when I move to Price Cell and write for example 5.00 Total Cell value changed to 5.00 and delete old value and also for Discount Cell and I can't did the equation to find the Total price for the category .
Richard MacCutchan 21-Nov-22 5:48am    
You should use the debugger to check the values returned from each of the above calls. Also do not use Convert.ToDouble as it can crash your application, use Double.TryParse, so you can capture any invalid numbers.
kudim 21-Nov-22 6:10am    
Thank you for replay Sir and thank you for your advice.
Sorry but I did not know anything about how to use debugger, can you please write an answer ?
Richard MacCutchan 21-Nov-22 6:18am    
In Visual Studio go to the edit page of the source file that contains the above code. Scroll down to the tb_TextChanged line. Click in the left margin and a breakpoint indicator should appear (probably on the first line below the method entry). Now run the code in Debug mode by pressing F5. When the tb_TextChanged event handler gets called the debugger should stop at that line. you can then examine all variables, single step your code etc.

It would also be better to capture the values of each cell that you reference in the above code, rather than doing dynamic calculations as you have. That way it is much easier to see where your mistakes may arise.

Learning to use the debugger is an essential skill for a developer so you should get familiar with all its features.

1 solution

 
Share this answer
 
Comments
kudim 21-Nov-22 5:44am    
Thank you for your replay , CellValueChanged , CellEndEdit , all they will work but the total value appear when you leave the cell while I need it like textChange event when I press write a number make the equation before I leave the Cell.

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