DatagridView的CellLeave光标离开响应事件,实现某列数字自动求和

时间:2022-02-17 13:42:05

DatagridView的CellLeave光标离开响应事件,实现某列数字自动求和

 

//光标离开DatagridView,循环获取DatagridView的每一行的第3列的值,相加传给重量
private void dgpz_dataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    this.dgpz_dataGridView.EndEdit();//光标离开,完成修改
    
    double zl = 0.0;
    int count = dgpz_dataGridView.Rows.Count;
    if (count > 0)
    {
        for (int i = 0; i < count; i++)
        {
            double dyslstr = 0.0;
            if (this.dgpz_dataGridView.Rows[i].Cells[2].Value == null)
            {
                dyslstr = 0.0;
            }
            else
            {
                dyslstr = double.Parse(this.dgpz_dataGridView.Rows[i].Cells[2].Value.ToString());
            }
            if (!dyslstr.ToString().Trim().Equals(""))
            {
                zl = zl + dyslstr;
            }
            
        }
    }
    this.sl.Text = zl.ToString();//多个品种的总调运量加在一起传给重量 

}

 

光标离开DatagridView后,实现效果:

DatagridView的CellLeave光标离开响应事件,实现某列数字自动求和