DataGridView中的更新数量不会更改总计 [英] Update Quantity inside of DataGridView not change the Total

查看:112
本文介绍了DataGridView中的更新数量不会更改总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。这是我的问题:



我的DataGridView中有这样的东西:



产品编号||数量||说明|| SubTotal ||总|| .......(等等)



产品代码的SM0001,
100的数量,
AS 5为说明,
10,000为副总计,1,000,000为总计



以上是正确的,因为 10,000 times 100 我们得到 1,000,000



我将数据添加到DataGridView中,当数据添加到DataGridView时,我点击DataGridView中的编辑,当我更改一些值时,将被更改和更新。但是,问题是当我尝试将数量从 100 更改为 500 时,没有更改总计总计假设根据更新和更改数量*子总计(因为我更改数量



这是怎么回事? >

当我尝试从DataGridView更新时,以上是我上面的问题的代码:

  private void DataGridViewCalculation(object sender,EventArgs e)
{
for(int i = 0; i< dataGridView1.Rows.Count; i ++)
{
int _quantity = Convert.ToInt32(this.dataGridView1.Rows [i] .Cells [1] .Value);
int _subTotal = Convert.ToInt32(this.dataGridView1.Rows [i] .Cells [3] .Value);
int _total = Convert.ToInt32(_quantity * _subTotal);

this.dataGridView1.Rows [i] .Cells [4] .Value = _total;
}

dataGridView1.DataSource = null;
dataGridView1.DataSource = _ds.Tables [0];
}

以下是我之前和之前的问题截图,我将数量更改为500从100:





当我没有将数量更改为 500 (仍为100)





当我已经更改了数量 500 ,但总计仍然与相同数量100



基本上,我希望当用户在DataGridView中点击EDIT并进行更改(假设用户在数量 DataGridView中,DataGridView中的总计也应该更改)



EDITED

  dataGri dView1.CellValueChanged + = new DataGridViewCellEventHandler(this.DataGridViewCalculation); 

private void DataGridViewCalculation(object sender,DataGridViewCellEventArgs e)
{
if(e.ColumnIndex.Equals(1))
{
int _total = Convert.ToInt32(dataGridView1 [Quantity,e.RowIndex] .Value)* Convert.ToInt32(dataGridView1 [SubTotal,e.RowIndex] .Value);
dataGridView1 [Total,e.RowIndex] .Value = _total.ToString();
}
}

注意 p>

当我尝试更改数量值时,编辑后的代码仍然不起作用, Total 仍然保持一致。

解决方案

您可以使用 CellValueChanged 事件。当单元格值更改时,此事件将被提升。
基本上你要做的是,




  • 检查数量单元格是否通过检查列索引来编辑

  • 使用<$ c $在同一行中获取 Sub Total 列和数量的值编辑的单元格中的c> RowIndex 。

  • 将两者相乘并设置为总计单元格



样本代码



<请注意,这是一个示例代码,您可能必须考虑在执行任何操作之前将值转换为所需的数字格式。

  private void dataGridView1_CellValueChanged(object sender,DataGridViewCellEventArgs e)
{
if(e.ColumnIndex.Equals(column Index of Quantity))
{
double total = Convert.ToDouble(dataGridView1 [Subtotal column name,e.RowIndex] .Value)* Convert.ToDouble(dataGridView1 [Quantity column name,e.RowIndex] .Value);
dataGridView1 [总列名称,e.RowIndex] .Value = total.ToString();
}
}


i have a problem. Here is my problem:

I got something like this in my DataGridView:

Product Code || Quantity || Description || SubTotal || Total || ....... (and so on)

SM0001 for the Product Code, 100 for the Quantity, AS 5 for the Description, 10,000 for the Sub Total, and 1,000,000 for the Total

The above it's correct because 10,000 times 100 we get 1,000,000

I add data to the DataGridView and when the data has been added to the DataGridView, i click edit in that DataGridView, when i change some value on it, it will be changed and updated. But, the problem is when i tried to change the "Quantity" from 100 to 500, it didn't change the Total, the Total suppose to update and change too based on Quantity * Sub Total (because i change the Quantity)

How is it like that?

Here is the code for my problem above when i tried to update it from DataGridView:

private void DataGridViewCalculation(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                int _quantity = Convert.ToInt32(this.dataGridView1.Rows[i].Cells[1].Value);
                int _subTotal = Convert.ToInt32(this.dataGridView1.Rows[i].Cells[3].Value);
                int _total = Convert.ToInt32(_quantity * _subTotal);

                this.dataGridView1.Rows[i].Cells[4].Value = _total;
            }

            dataGridView1.DataSource = null;
            dataGridView1.DataSource = _ds.Tables[0];
        }

Here is the screenshot of my problem above after and before i change the quantity to 500 from 100:

Screenshot above display when i haven't change the Quantity to 500 (still at 100)

Screenshot above display when i already changed the Quantity to 500, but the Total still the same as Quantity 100

Basically, i want when user click EDIT in DataGridView and made changes in it (let's say user make changes on Quantity in DataGridView, the Total in DataGridView should change too)

EDITED:

dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(this.DataGridViewCalculation);

private void DataGridViewCalculation(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex.Equals(1))
            {
                int _total = Convert.ToInt32(dataGridView1["Quantity", e.RowIndex].Value) * Convert.ToInt32(dataGridView1["SubTotal", e.RowIndex].Value);
                dataGridView1["Total", e.RowIndex].Value = _total.ToString();
            }
        }

NOTE:

The edited code still not working when i tried to change the Quantity value, the Total still remains same.

解决方案

You can use CellValueChanged event. This event will be raised when cell value is changed. Basically what you have to do is,

  • Check if the Quantity cell is edited by checking column index
  • Get the value of Sub Total column and Quantity in the same row using RowIndex of the edited cell.
  • Multiply both and set it to the Total cell.

Sample Code

Please note that this is a sample code, You may have to consider converting values to the required numeric format before performing any operation.

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex.Equals("column Index of Quantity"))
        {
            double total = Convert.ToDouble(dataGridView1["Subtotal column name", e.RowIndex].Value) * Convert.ToDouble(dataGridView1["Quantity column name", e.RowIndex].Value);
            dataGridView1["Total Column name", e.RowIndex].Value = total.ToString();
        }            
    }

这篇关于DataGridView中的更新数量不会更改总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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