如何使用具有相同add_date的datagridview计算总计列的总计?

时间:2019-05-05 08:12:34

标签: c# visual-studio winforms

我希望代码使用相同的添加日期列来计算总计。像每日销售报告准备一样。我必须在这张照片中计算具有相同添加日期的总计总计。但是使用此代码,我得到的是“对象引用未设置为对象的实例。

DatagridView image

enum GVColumns
    {
        //We make an enum for our GridView columns
        id = 0,
        bill_date,
        bill_no,
        cust_name,           
       sub_total,
        discount,
        grand_total,
        credit,            
        added_by
    }


   private void btnsubtootal_Click(object sender, EventArgs e)
    {
        decimal grandtotalByDate = 0;
        DateTime searchedDate = new DateTime();

        foreach (DataGridViewRow row in dgvSales.SelectedRows)
        {
            var txtDateAddedValue = row.Cells[(int)GVColumns.bill_date].Value.ToString();
            searchedDate = DateTime.Parse(txtDateAddedValue); //get our "bill_date" to search
        }

        foreach (DataGridViewRow row in dgvSales.Rows)
        {
            var txtGrandTotalValue = row.Cells[(int)GVColumns.grand_total].Value.ToString(); //"grand_total" cell value as string
            var txtDateAddedValue = row.Cells[(int)GVColumns.bill_date].Value.ToString(); //"added_date" cell value as string
            var rowDateAddedValue = DateTime.Parse(txtDateAddedValue);

            //check if our dates match
            if (rowDateAddedValue.Date == searchedDate.Date)
            {
                //add "grand_total" values if dates match
                grandtotalByDate += decimal.Parse(txtGrandTotalValue);
            }
        }

        txtNetSales.Text = grandtotalByDate.ToString("N2");

0 个答案:

没有答案