Click here to Skip to main content
14,692,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to multiply 2 columns and show the value in the third column automatically in datagridview C#

example: i have these 3 columns in the datagirdview: Qty Price Sub-Total

i want: Qty * Price = Sub-Total

so what exactly i want is when a user enter a qty and price the subtotal cell is calculated automatically

I am a newbie so please answer with details so i can understand :)
Posted
Updated 7-May-13 0:33am
v2

1 solution

Use Datagridview's CurrentCellDirtyStateChanged event
Private void dgv1_CurrentCellDirtyStateChanged(Object sender, System.EventArgs e) 
{
            If (dgv1.IsCurrentCellDirty)
            {
                dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit)
                If (dgv1.CurrentCell.ColumnIndex == 1 || dgv1.CurrentCell.ColumnIndex == 2)//columnindex of qty & rate column
                {  
                    decimal qty = 0.0;
                    decimal Rate= 0.0;
                    if(Decimal.TryParse(dgv1.Rows[dgv1.CurrentCell.RowIndex].Cells["ColQty"].Value, qty)==true)
                    {
                        qty = dgv1.Rows[dgv1.CurrentCell.RowIndex].Cells["ColQty"].Value;
                    }
                   if(Decimal.TryParse(dgv1.Rows[dgv1.CurrentCell.RowIndex].Cells["ColRate"].Value, Rate)==true)
                    {
                        Rate = dgv1.Rows[dgv1.CurrentCell.RowIndex].Cells["ColRate"].Value;
                    }
                 dgv1.Rows[dgv1.CurrentCell.RowIndex].Cells["ColTotal"].Value =  qty * Rate;
                }
            }
} 

Happy Coding!
:)
   
v2
Comments
dodez1 7-May-13 5:19am
   
it didnt work with me :(
Aarti Meswania 7-May-13 5:28am
   
what is error?
dodez1 7-May-13 5:39am
   
Operator '*' cannot be applied to operands of type 'object' and 'object'

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