datagridview单元格间自动计算

datagridview单元格之间的自动计算,今天在这个网站上看到的,稍微修改了下,新手有些地方没弄懂~!
原文地址:https://blog.csdn.net/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;
            }
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: DataGridView是Windows Forms中的一个控件,用于在一个表格中显示和编辑数据。要在DataGridView单元格中添加控件,可以通过自定义单元格类型来实现。 首先,我们可以继承自DataGridViewTextBoxCell类来创建一个自定义的单元格类型,例如MyDataGridViewButtonCell。在这个自定义单元格类型中,我们可以重写Paint方法来绘制指定的控件,如按钮或复选框,并提供相应的事件处理方法。 然后,我们需要创建一个自定义的列类型,例如MyDataGridViewButtonColumn,继承自DataGridViewColumn类。在这个自定义列类型中,我们可以指定使用之前创建的自定义单元格类型。 接下来,我们可以在窗体的加载事件或其他需要的地方,创建一个DataGridView实例,并添加自定义的列类型。 例如,我们可以这样添加一个带有按钮的列到DataGridView中: ``` // 创建自定义列类型 MyDataGridViewButtonColumn buttonColumn = new MyDataGridViewButtonColumn(); buttonColumn.HeaderText = "操作"; buttonColumn.Text = "按钮"; // 添加列到DataGridView dataGridView.Columns.Add(buttonColumn); ``` 然后,我们可以通过数据绑定或手动添加行的方式向DataGridView中添加数据。当需要在特定单元格中显示控件时,可以通过获取该单元格的实例,使用Controls属性或者其他方法来添加控件。 例如,我们可以这样在第三行第二列的单元格中添加一个按钮: ``` // 获取指定单元格 DataGridViewCell cell = dataGridView.Rows[2].Cells[1]; // 创建和添加控件 Button button = new Button(); button.Text = "点击"; cell.Controls.Add(button); ``` 通过以上方法,我们可以在DataGridView单元格中添加不同类型的控件,并实现相应的事件处理逻辑。需要注意的是,在处理控件的事件时,可能需要获取到相应单元格的数据或其他相关信息。 ### 回答2: DataGridView控件是Windows Forms中常用的一个控件,用于展示和编辑数据。在DataGridView中添加控件到单元格主要有两种方式。 第一种方式是直接在DataGridView的某个单元格中添加控件。可以通过在DataGridView的CellFormatting事件中,判断需要添加控件的条件,然后通过e.CellStyle属性中的Controls属性,添加控件到指定的单元格中。例如,可以通过如下代码实现在第一列中添加一个Button控件: private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 0 && e.RowIndex >= 0) { DataGridViewButtonCell buttonCell = new DataGridViewButtonCell(); buttonCell.Value = "按钮"; dataGridView1[e.ColumnIndex, e.RowIndex] = buttonCell; } } 第二种方式是使用DataGridView的EditingControlShowing事件,通过该事件获取编辑单元格中的编辑控件,然后对编辑控件进行自定义设置。例如,可以通过如下代码在编辑单元格中添加一个DateTimePicker控件: private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (e.Control is DateTimePicker) { DateTimePicker dtp = (DateTimePicker)e.Control; dtp.Format = DateTimePickerFormat.Short; dtp.ShowUpDown = true; } } 以上就是在DataGridView单元格中添加控件的两种方式。通过这两种方式,可以实现在DataGridView中加入各种控件,以满足数据展示和编辑的需求。 ### 回答3: DataGridView是.NET中常用的控件之一,用于显示和编辑表格数据。在DataGridView中,我们可以通过给单元格添加控件来实现一些特殊的功能或交互效果。 要在DataGridView单元格中添加控件,首先需要创建一个自定义的控件,例如一个按钮、文本框或下拉列表框。然后,可以通过以下步骤将控件添加到指定的单元格中: 1. 在DataGridView的CellFormatting事件中,判断当前要显示的单元格是否是需要添加控件的单元格,通过CurrentCell地址获取所需的行和列索引。 2. 使用DataGridView的GetCellDisplayRectangle方法获取单元格的位置和大小信息。 3. 创建自定义控件的实例,并设置其属性和事件。 4. 设置控件的位置和大小,使之与单元格的位置和大小相匹配。 5. 通过DataGridView的Controls集合,将控件添加到DataGridView的容器中。 这样,当DataGridView绘制时,控件就会出现在指定的单元格中。用户可以与控件进行交互,从而实现特定的功能。 注意,当用户在单元格中编辑或选择其他单元格时,需要相应地更新控件的状态或删除已添加的控件。可以通过DataGridView的CellEndEdit事件或SelectionChanged事件来处理相应的逻辑。 总之,通过在DataGridView单元格中添加自定义控件,可以为用户提供更灵活和交互性的界面,实现一些特殊的功能和效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值