想实现库存录入后的自动计算,如何实现;

请教个问题,针对库存,我设定了个计算公式,上一行的库存+当前行入库-当前行的出库=当前行的库存,每一类物料的最后一行,都是该物料的最新库存;我将公式写在了datagridveiw的CellEndEdit事件下面;

但这样每当我新增时,新增的行,必须要点击单元格,才会自动计算,比较麻烦,请问用什么方法,能新增录入行后,自动用上述公式进行计算呢;我后来改在datagridview的rowadd事件下,及CellValueChanged事件下,都不行,都无法自动计算,相关问题截图,及代码如下,请指导一下;

img

img

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (e.RowIndex < 1)
                {
                    MessageBox.Show("请从第二行录入数据");

                    return;
                }

                string PreMaterial = dataGridView1.Rows[e.RowIndex - 1].Cells[2].Value.ToString();

                string CurMaterial = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();


                if (PreMaterial != CurMaterial)
                {
                    return;
                }

                else
                {

                    if ((e.RowIndex > 0) && (e.ColumnIndex == 4 || e.ColumnIndex == 5)) // 不是第一行
                    {
                        int rowIndex = e.RowIndex;

                        decimal 上一行结存数量 = Convert.ToDecimal(dataGridView1.Rows[rowIndex - 1].Cells[6].Value);

                        decimal 当前行入库数量, 当前行出库数量;

                        if (dataGridView1.Rows[rowIndex].Cells[4].Value == DBNull.Value)
                        {
                            当前行入库数量 = 0;
                        }

                        else
                        {
                            当前行入库数量 = Convert.ToDecimal(dataGridView1.Rows[rowIndex].Cells[4].Value);
                        }

                        if (dataGridView1.Rows[rowIndex].Cells[5].Value == DBNull.Value)
                        {
                            当前行出库数量 = 0;
                        }

                        else
                        {
                            当前行出库数量 = Convert.ToDecimal(dataGridView1.Rows[rowIndex].Cells[5].Value);
                        }

                        decimal 当前行结存数量 = 上一行结存数量 + 当前行入库数量 - 当前行出库数量;

                        当前行结存数量 = Math.Round(当前行结存数量, 2, MidpointRounding.AwayFromZero);
                        //当前行结存数量 = (float)Math.Round(上一行结存数量, 2);

                        dataGridView1.Rows[rowIndex].Cells[6].Value = 当前行结存数量.ToString("0.00");
                        //dataGridView1.Rows[rowIndex].Cells[6].Value = 上一行结存数量.ToString("0.00");
                    }

                }

            }  
        }

你这个问题应该问了几次了,这个想法应该是实现不了的
原因如下
在输入的时候能监测的事件无非就是键盘按下、抬起的几个相关事件
能用上的只有KeyUp(键盘抬起事件)
但即使你监测这个事件 因为文本框中输入的值一直不会得到更新,所以你监测KeyUp事件也是没有意义的(例如一个单元格的值是1在你输入时只要没有点击其他区域或者是按回车 单元格的值一直是1)
关于 CellValueChanged、CellEndEdit这两个事件

  • 在用户输入完毕后 假如文本框中的值发生了变化优先执行CellValueChanged 然后才会执行 CellEndEdit
  • 如果 文本框中的值没有变化 则只触发CellEndEdit

其实基于上面的说法可能有一个能实现的办法,但是非常麻烦,理论上应该可以
在监测 KeyUp 事件中拿到用户输入的按钮,然后转换成相应的值,在将当前有输入焦点的单元格的现有值与输入的值进行拼接,然后转换成你需要的值,在进行你想要的计算 理论上是可行的


假如你想尝试试上面的理论的话
需要监测的是
dataGridView的KeyUp 事件好像监测不到,不知道为什么,但可以监测以下事件

//这里的this是Form窗体的实例
this.KeyUp += (sender, e) =>
            {
                
            };

【以下回答由 GPT 生成】

问题标题:如何实现库存录入后的自动计算? 问题标签:java

问题内容: 我想在录入库存后自动计算库存,我已经定义了一个计算公式,即“上一行库存 + 当前行入库 - 当前行出库 = 当前行库存”。每个物料的最后一行都是该物料的最新库存。我已经将这个公式写在datagridview的CellEndEdit事件中。

然而,每当我新增一行时,新增的行只有在点击单元格后才会自动计算,这比较麻烦。请问有什么方法可以实现在新增录入行后自动使用上述公式进行计算呢?我尝试过在datagridview的rowadd事件和CellValueChanged事件中进行计算,但都无法自动计算。

以下是相关问题的截图和代码,请指导一下:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (e.RowIndex < 1)
        {
            MessageBox.Show("请从第二行录入数据");
            return;
        }

        string PreMaterial = dataGridView1.Rows[e.RowIndex - 1].Cells[2].Value.ToString();

        string CurMaterial = dataGridView1.Rows[e.

解决方案

要实现库存录入后的自动计算,可以在DataGridView控件的RowAdded事件中进行计算。在该事件中,获取当前行的入库和出库数据,然后根据公式进行计算并更新当前行的库存。以下是一个可能的解决方案:

private void dataGridView1_RowAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    if (e.RowIndex > 0)
    {
        DataGridViewRow currentRow = dataGridView1.Rows[e.RowIndex];
        DataGridViewRow previousRow = dataGridView1.Rows[e.RowIndex - 1];

        // 获取上一行的库存
        decimal previousInventory = decimal.Parse(previousRow.Cells["Inventory"].Value.ToString());

        // 获取当前行的入库和出库
        decimal currentIncoming = decimal.Parse(currentRow.Cells["Incoming"].Value.ToString());
        decimal currentOutgoing = decimal.Parse(currentRow.Cells["Outgoing"].Value.ToString());

        // 计算当前行的库存
        decimal currentInventory = previousInventory + currentIncoming - currentOutgoing;

        // 更新当前行的库存
        currentRow.Cells["Inventory"].Value = currentInventory;
    }
}

请注意,在上述代码中,我假设你的DataGridView控件中有名为"Incoming"、"Outgoing"和"Inventory"的列来分别表示入库、出库和库存。你可以根据实际情况修改这些列的名称以适应你的DataGridView控件。

将上述代码放入你的窗体类中,然后将DataGridView的RowAdded事件与上述方法进行绑定。这样,每当新增一行时,库存将会自动计算并显示在相应行的库存列中。

希望这个解决方案对你有帮助!如果你有其他问题,请随时提问!


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^