GridView ASP.Net C# 中的乘法和求和

Multiplication and Sum in GridView ASP.Net C#

我有一个绑定到 GridView 数据源的数据表,如下所示。

总体而言,我想将 'Quantity' 列值与 'Part1 qty' 列值相乘,直到 'column5' 单元格值重复,依此类推 操作结果应显示在以红色突出显示的值下方以供理解

My GridView data currently

我想要以下输出

Required Output

我的网格标记

My GridMarkup

到目前为止我所做的是

protected void GridView1_DataBound(object sender, EventArgs e)
    {
        int gridViewCellCount = GridView1.Rows[0].Cells.Count;
        string[] columnNames = new string[gridViewCellCount];
        for (int k = 0; k < gridViewCellCount; k++)
        {
            columnNames[k] = ((System.Web.UI.WebControls.DataControlFieldCell)(GridView1.Rows[0].Cells[k])).ContainingField.HeaderText;
        }

        for (int i = GridView1.Rows.Count - 1; i > 0; i--)
        {
            GridViewRow row = GridView1.Rows[i];
            GridViewRow previousRow = GridView1.Rows[i - 1];
            
            var result = Array.FindIndex(columnNames, element => element.EndsWith("QTY"));
            var Arraymax=columnNames.Max();
            int maxIndex = columnNames.ToList().IndexOf(Arraymax);
            decimal MultiplicationResult=0;
            int counter = 0;

            for (int j = 8; j < row.Cells.Count; j++)
            {
                if (row.Cells[j].Text == previousRow.Cells[j].Text)
                {
                    counter++;
                    if (row.Cells[j].Text != "&nbsp;" && result < maxIndex)
                    {
                        var Quantity = GridView1.Rows[i].Cells[1].Text;
                        var GLQuantity = GridView1.Rows[i].Cells[result].Text;
                        var PreviousQuantity= GridView1.Rows[i-1].Cells[1].Text;
                        var PreviousGLQuantity= GridView1.Rows[i-1].Cells[result].Text;
                        //var Quantity = dt.Rows[i].ItemArray[1];
                        //var GLQuantity = dt.Rows[i].ItemArray[Convert.ToInt64(result)].ToString();
                        var GLQ = GLQuantity.TrimEnd(new Char[] { '0' });
                        var PGLQ = PreviousGLQuantity.TrimEnd(new char[] { '0' });
                        if (GLQ == "")
                        {
                            GLQ = 0.ToString();
                        }
                        if (PGLQ == "")
                        {
                            PGLQ = 0.ToString();
                        }

                        MultiplicationResult = Convert.ToDecimal(Quantity) * Convert.ToDecimal(GLQ) + Convert.ToDecimal(PreviousQuantity) * Convert.ToDecimal(PGLQ);

                        object o = dt.Rows[i].ItemArray[j] + " " + MultiplicationResult.ToString();
                        
                        GridView1.Rows[i].Cells[j].Text = o.ToString();
                        GridView1.Rows[i].Cells[j].Text.Replace("\n", "<br/>");
                        result++;

                    }
                    else
                        result++;

                    if (previousRow.Cells[j].RowSpan == 0)
                    {
                        if (row.Cells[j].RowSpan == 0)
                        {
                            previousRow.Cells[j].RowSpan += 2;
                           
                        }
                        else
                        {
                            previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;

                        }
                        row.Cells[j].Visible = false;

                    }

                   
                }

                else
                    result++;
            }
        }
       
    }

提前致谢。

我们可以使用下面的答案

protected void GridView1_DataBound(object sender, EventArgs e)
    {
        int gridViewCellCount = GridView1.Rows[0].Cells.Count;
        string[] columnNames = new string[gridViewCellCount];
        for (int k = 0; k < gridViewCellCount; k++)
        {
            columnNames[k] = ((System.Web.UI.WebControls.DataControlFieldCell)(GridView1.Rows[0].Cells[k])).ContainingField.HeaderText;
        }

        for (int i = GridView1.Rows.Count - 1; i > 0; i--)
        {
            GridViewRow row = GridView1.Rows[i];
            GridViewRow previousRow = GridView1.Rows[i - 1];

            var result = Array.FindIndex(columnNames, element => element.EndsWith("QTY"));
            var Arraymax = columnNames.Max();
            int maxIndex = columnNames.ToList().IndexOf(Arraymax);
            decimal MultiplicationResult = 0;
            decimal currentCellResult = 0;
           

            for (int j = 8; j < row.Cells.Count; j++)
            {
                var defaultvalue = row.Cells[j].Text.ToString();
                var defaultvalueArray = defaultvalue.Split(' ');
                var originalMultiplicationResult = defaultvalueArray.Count() == 2 ? defaultvalueArray.Last() : "0";
                var originalCellValue = defaultvalueArray.Count() == 2 ? defaultvalueArray.First() : row.Cells[j].Text.ToString();
                if (originalCellValue == previousRow.Cells[j].Text)
                {
                    
                    if (row.Cells[j].Text != "&nbsp;" && result < maxIndex)
                    {
                        var Quantity = GridView1.Rows[i].Cells[1].Text;
                        var GLQuantity = GridView1.Rows[i].Cells[result].Text;
                        var PreviousQuantity = GridView1.Rows[i - 1].Cells[1].Text;
                        var PreviousGLQuantity = GridView1.Rows[i - 1].Cells[result].Text;
                        var GLQ = GLQuantity.TrimEnd(new Char[] { '0' });
                        var PGLQ = PreviousGLQuantity.TrimEnd(new char[] { '0' });
                        if (GLQ == "")
                        {
                            GLQ = 0.ToString();
                        }
                        if (PGLQ == "")
                        {
                            PGLQ = 0.ToString();
                        }
                        currentCellResult = Convert.ToDecimal(Quantity) * Convert.ToDecimal(GLQ);
                        MultiplicationResult = currentCellResult + Convert.ToDecimal(PreviousQuantity) * Convert.ToDecimal(PGLQ);
                        if (row.Cells[j].RowSpan == 0)
                        {
                            //DataTable dt = (DataTable)ViewState["dt"];
                            object o = dt.Rows[i].ItemArray[j] + " " + MultiplicationResult.ToString();
                            
                            previousRow.Cells[j].Text = o.ToString();
                            //previousRow.Cells[j].Text = previousRow.Cells[j].Text.Split("");

                        }
                        else
                        {
                            //DataTable dt = (DataTable)ViewState["dt"];
                            var t = Convert.ToDecimal(originalMultiplicationResult) - Convert.ToDecimal(currentCellResult) + MultiplicationResult;
                            object o = dt.Rows[i].ItemArray[j] + " " + t.ToString();
                            previousRow.Cells[j].Text = o.ToString();
                            //previousRow.Cells[j].Text.Replace("\n", "<br>");
                        }
                        result++;

                    }
                    else
                        result++;

                    if (previousRow.Cells[j].RowSpan == 0)
                    {
                        if (row.Cells[j].RowSpan == 0)
                        {
                            previousRow.Cells[j].RowSpan += 2;
                        }
                        else
                        {
                            previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                        }
                        row.Cells[j].Visible = false;
                    }
                }

                else
                    result++;
            }
        }
    }