datagridview单元格间自动计算

本文介绍了一种在DataGridView中实现单元格自动计算的方法,通过在CellParsing和CellEndEdit事件中进行数值转换和计算,确保在编辑单元格时能够实时更新相关联的计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

datagridview单元格之间的自动计算,今天在这个网站上看到的,稍微修改了下,新手有些地方没弄懂~!
原文地址:https://blog.youkuaiyun.com/sunferny/article/details/4440722

private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            if (e.ColumnIndex == 14 || e.ColumnIndex == 15)
            {
                DataGridViewRow theCurrentRow = this.dataGridView1.CurrentRow;
                if (!string.IsNullOrEmpty(theCurrentRow.Cells[14].Value.ToString()))
                {
                    if (!string.IsNullOrEmpty(theCurrentRow.Cells[15].Value.ToString()))
                    {
                        int accountA = 0;
                        int accountB = 0;
                        if (e.ColumnIndex == 14)
                        {
                            accountA = Convert.ToInt32(e.Value.ToString());
                        }
                        else
                        {
                            accountA = Convert.ToInt32(theCurrentRow.Cells["B01C004"].Value);
                        }

                        if (e.ColumnIndex == 15)
                        {
                            accountB = Convert.ToInt32(e.Value.ToString());
                        }
                        else
                        {
                            accountB = Convert.ToInt32(theCurrentRow.Cells["B01C005"].Value);
                        }

                        int accountC = accountA + accountB;
                        theCurrentRow.Cells["B01C006"].Value = accountC;
                        
                        //强制控件使其工作区无效并立即重绘自己和任何子控件
						this.dataGridView1.Refresh();
                        
                        e.ParsingApplied = true;
                        
                    }

运行程序可行,但是存在问题是在结束编辑的时候提取出的是修改前的数值,且其中一个数值为空不会计算。把代码放到CellEndEdit事件中,就解决提取修改前的数值问题了。

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 14 || e.ColumnIndex == 15)
            {
                double accountA = 0;
                double accountB = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    
                    accountA = Convert.ToDouble(dataGridView1.Rows[i].Cells[14].Value);
                    accountB = Convert.ToDouble(dataGridView1.Rows[i].Cells[15].Value);
                    double accountC = accountA + accountB;
                    dataGridView1.Rows[i].Cells[16].Value = accountC;
                }
            }
        }

前提是单元格内不能为空,至少得是0。
————————————————————————————————————————
加上下面的代码在单元格输入空白的时候赋值0,避免单元格计算出错。

if ((string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue == "")
            {
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 0;
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值