【问题标题】:Displaying data in the Data Grid View? Access, C#在数据网格视图中显示数据?访问,C#
【发布时间】:2014-04-21 17:10:07
【问题描述】:

我想计算总价并将其显示在数据网格视图中。第四列没有从数据库中检索信息,所以我不知道该怎么做。下面是 GUI 的样子:

这是我到目前为止的代码......

                double colTotal = 0; //set column total to 0

                foreach (DataRow currentRow in itemDS.Tables[0].Rows)
                {
                    // add to data grid view
                    invoiceDataGridView.Rows.Add(currentRow[1].ToString(), currentRow[2].ToString(), currentRow[3].ToString());

                    //Decalare variables for item quantity and price
                    var itemQuant = Convert.ToDouble(currentRow[1]);
                    var priceEach = Convert.ToDouble(currentRow[3]);

                    //Multiply the item quantity by the item price
                    double subTotal = itemQuant * priceEach;

                    //do this for each row
                    colTotal += subTotal;
                }

                //Display subtotal
                subTotalOutput.Text = colTotal.ToString();
                //Calculate tax
                double tax = colTotal * .073;
                //Display total tax
                taxOutput.Text = Convert.ToString(tax);
                //Add the subtotal and the total tax
                double totPrice = colTotal + tax;
                //Display total price
                totalOutput.Text = Convert.ToString(totPrice);

            }

【问题讨论】:

    标签: c# database visual-studio-2010 ms-access


    【解决方案1】:

    我相信您可以在填充网格之前进行 subTotal 计算,然后将 subTotal 变量添加到您对 dataGrid.Rows.Add() 的调用中。像这样:

    foreach (DataRow currentRow in itemDS.Tables[0].Rows)
    {
        //Decalare variables for item quantity and price
        var itemQuant = Convert.ToDouble(currentRow[1]);
        var priceEach = Convert.ToDouble(currentRow[3]);
    
        //Multiply the item quantity by the item price
        double subTotal = itemQuant * priceEach;
    
    
        // add to data grid view
        invoiceDataGridView.Rows.Add(currentRow[1].ToString(), currentRow[2].ToString(), currentRow[3].ToString(), subTotal);
    
        //do this for each row
        colTotal += subTotal;
    }
    

    【讨论】:

      【解决方案2】:

      我首先要说的是,您可以使用 DataGridView 控件的 DataSource 属性让您的生活更轻松,但是要根据您当前的设置解决您的问题,我只需移动一些代码并像这样进行操作:

      double subTotal = itemQuant * priceEach;
      
      invoiceDataGridView.Rows.Add(currentRow[1].ToString(), currentRow[2].ToString(), currentRow[3].ToString(), subTotal.ToString());
      

      【讨论】:

      • 非常感谢。一开始我不确定在哪里添加这个,但我明白了。谢谢你:)
      猜你喜欢
      • 2017-01-06
      • 2013-12-06
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多