Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating windows application using c# 2010, In my application I am using datagridview for billing purpose, here my grid view having 6 cells.

I am entered first time cell values on 2rd & 3rd cells that time cell calculating values shown on 5th cell. It’s correct,

But again second time I am entered the values on cell 2rd, 3rd & 4th cell that time calculating values shown only 6th cell but calculating results shown on again 5th cell, I want my calculating values shown on 6th cell only. Don't want again 5th cell.

How to solve this error. Any one give me ideas.

What I have tried:

Data Grid View Calculating cell values problem

[EDIT OP code from comments]
Thiis is my code

C#
foreach (DataGridViewRow row in dataGridView1.Rows)
{
   if (row.Cells[4].Value != DBNull.Value && row.Cells[6].Value != DBNull.Value)
   {
      float iWei = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());

      float iPer = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());

      dataGridView1.Rows[e.RowIndex].Cells[5].Value = (iWei * iPer) / 100;
   }
   else
   {
      float iWei = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());

      float iPer = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());

      float iWas = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());

      dataGridView1.Rows[e.RowIndex].Cells[6].Value = (iPer + iWas) * iWei;
   }
}
}
Posted
Updated 6-Dec-16 3:56am
v3
Comments
CHill60 5-Dec-16 18:55pm    
You will actually have to show us the code that is doing the calculation!
Remember we can't see your screen, examine your HDD, or read your mind.
Boopalslm 6-Dec-16 4:02am    
this is my code

foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[4].Value != DBNull.Value && row.Cells[6].Value != DBNull.Value)
{
float iWei = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());

float iPer = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());


dataGridView1.Rows[e.RowIndex].Cells[5].Value = (iWei * iPer) / 100;


}


else
{


float iWei = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());

float iPer = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());

float iWas = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());



dataGridView1.Rows[e.RowIndex].Cells[6].Value = (iPer + iWas) * iWei;

}
}
}
CHill60 6-Dec-16 9:53am    
If you had used the Improve question link you would not have had to post the code 3 times! Does Solution 1 solve your problem?
Cristina Carrasco Angulo 5-Dec-16 19:52pm    
Can you show the code please?
Boopalslm 6-Dec-16 4:02am    
this is my code


foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[4].Value != DBNull.Value && row.Cells[6].Value != DBNull.Value)
{
float iWei = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());

float iPer = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());


dataGridView1.Rows[e.RowIndex].Cells[5].Value = (iWei * iPer) / 100;


}


else
{


float iWei = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());

float iPer = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());

float iWas = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());



dataGridView1.Rows[e.RowIndex].Cells[6].Value = (iPer + iWas) * iWei;

}
}
}

1 solution

Okay, you want to show calculation result in 5th cell when you enter values in 2nd and 3rd cell.
and if you enter values in 2nd, 3rd and 4th cell, you want to show calculation result in 6th cell only and not in 5th cell.

Am i right?
If this is what you want then try this code...

C#
foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.Cells[2].Value != DBNull.Value && row.Cells[3].Value != DBNull.Value)
            {
                if(row.Cells[4].Value ==DBNull.Value)
                {
                    float iWei = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());
                    float iPer = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());
                    dataGridView1.Rows[e.RowIndex].Cells[5].Value = (iWei * iPer) / 100;
                    dataGridView1.Rows[e.RowIndex].Cells[6].Value = null;//you can remove it if not needed
                }
                else
                {
                    float iWei = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());
                    float iPer = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString());
                    float iWas = float.Parse(dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString());
                    dataGridView1.Rows[e.RowIndex].Cells[6].Value = (iPer + iWas) * iWei;
                    dataGridView1.Rows[e.RowIndex].Cells[5].Value = null;//you can remove it if not needed
                }
            }
        }



Let me know if this solve your problem...
Happy coding :)
 
Share this answer
 
Comments
Boopalslm 6-Dec-16 11:08am    
In your if condition code cannot multiplication value shown on cell 5, how to solve the error.
Boopalslm 6-Dec-16 11:14am    
i am put a break point in foreach but the script cannot go to inside if condition jump on else part.
CHill60 6-Dec-16 12:13pm    
Which means tht row.Cells[4].Value is not equal to DBNull.Value
Boopalslm 6-Dec-16 12:16pm    
is not per-mentally null some times enter the values in cell 4
CHill60 6-Dec-16 12:17pm    
As I said - it's NOT null therefore the code will flow into the else block of the statement

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