Click here to Skip to main content
14,775,019 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

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 4: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;

}
}
}
Awesh Vishwakarma 6-Dec-16 3:19am
   
Post a snippet of your code, if you really want the help...
Boopalslm 6-Dec-16 4:07am
   
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;

}
}
}
Awesh Vishwakarma 6-Dec-16 5:43am
   
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?

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...

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 :)
   
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
Boopalslm 6-Dec-16 12:22pm
   
yes if condition code 4 cell null, but cannot result shown on 5th cell,
Boopalslm 6-Dec-16 12:26pm
   
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
}

above code correct, but in 4th cell empty, that time cannot shown calculating values in 5th cell

that is my problem.
CHill60 6-Dec-16 12:28pm
   
You said the flow jumps to the else part, therefore that value is not null therefore you will set Cells[5] to null and Cells[6] to the value.

Note Cells[5] is the 6th cell, the 5th cell is the value iWas ...
Collections are base 0
Cells[0] - first cell
Cells[1] - second cell
Cells[2] - 3rd cell etc etc
Boopalslm 6-Dec-16 12:32pm
   
sir else part working perfectly, problem in if condition
Boopalslm 6-Dec-16 12:34pm
   
now i got a solution, this is my code working good.

try
{

foreach (DataGridViewRow row in dataGridView1.Rows)
{

if (String.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells[4].Value as String))

{
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 if (row.Cells[4].Value != null)
{

dataGridView1.Rows[e.RowIndex].Cells[5].Value = "0";

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

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

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



dataGridView1.Rows[e.RowIndex].Cells[6].Value = (iPers + iWass) * iWeis;


}
}
}
catch (Exception)
{
}
Boopalslm 6-Dec-16 12:36pm
   
but i got another problem

once stored the data grid view values into database, again I am bind the grid values and Insert into new one row but data grid view calculation is working wrong, first time working is good, once stored and retrieve and insert new row calculation problem, and I am write the code CellEndEdit – Events.

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