在datagridview中乘以2列 [英] multiply 2 columns in datagridview

查看:114
本文介绍了在datagridview中乘以2列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将2列相乘并在datagridview中自动显示第三列中的值C#



示例:我在datagirdview中有以下3列:Qty Price Sub -Total



i想要:数量*价格=小计



所以我想要的是什么时候用户输入数量和价格自动计算小计单元格



我是新手所以请回答详细信息,以便我能理解:)

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 :)

推荐答案

使用Datagridview的 CurrentCellDirtyStateChanged 事件

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!
:)


这篇关于在datagridview中乘以2列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆