无法在asp.net c#

时间:2018-05-10 12:39:19

标签: c# asp.net .net gridview

我正在尝试在下面提到的代码中添加gridviewrow作为小计,并且它不允许我在AddTotalRow方法中将其添加到此行中。它说下面的错误。

gvData.Rows.Add(row);

enter image description here

    string currentYear = string.Empty;
    int subTotal, subTotalCompleted = 0;
    int subTotalRowIndex = 0;

    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        subTotal = 0;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataTable dt = (e.Row.DataItem as DataRowView).DataView.Table;
            string year = dt.Rows[e.Row.RowIndex]["year"].ToString();
            if (year != currentYear)
            {
                if (e.Row.RowIndex > 0)
                {
                    for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++)
                    {
                        subTotal += Convert.ToInt32(gvData.Rows[i].Cells[2].Text);
                        subTotalCompleted += Convert.ToInt32(gvData.Rows[i].Cells[3].Text);
                    }
                    this.AddTotalRow("Sub Total", subTotal.ToString("C2"), subTotalCompleted.ToString("C2"));
                    subTotalRowIndex = e.Row.RowIndex;
                }
                currentYear = year;
            }
        }
    }

    private void AddTotalRow(string labelText, string value, string completed)
    {
        GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
        row.BackColor = ColorTranslator.FromHtml("#F9F9F9");
        row.Cells.AddRange(new TableCell[4] {
                           new TableCell(),
                           new TableCell{ Text = labelText, HorizontalAlign = HorizontalAlign.Right },
                           HyperLinkCell(value, "http://www.yahoo.com"),
                           HyperLinkCell(completed, "http://www.google.com")});
        gvData.Controls[0].Controls.Add(row);
    }

    protected TableCell HyperLinkCell(string text, string url)
    {
        TableCell cell = new TableCell();
        HyperLink link = new HyperLink();
        try
        {
            link.Text = text;
            link.Font.Underline = true;
            link.Target = "_blank";
            link.NavigateUrl = url;
            link.Attributes.Add("style", "color:Black;");
            cell.Controls.Add(link);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return cell;
    }

    protected void OnDataBound(object sender, EventArgs e)
    {
        for (int i = subTotalRowIndex; i < gvData.Rows.Count; i++)
        {
            subTotal += Convert.ToInt32(gvData.Rows[i].Cells[2].Text);
            subTotalCompleted += Convert.ToInt32(gvData.Rows[i].Cells[3].Text);
        }
        this.AddTotalRow("Sub Total", subTotal.ToString("C2"), subTotalCompleted.ToString("C2"));
    }

有人可以帮我纠正这个问题吗?

更新1:

完成列的小计及其第2小计总结了第一小计,不应该发生。

已完成的小计有1,998.00,而应该是1411.00。请查看下面的图片以供参考。

enter image description here

0 个答案:

没有答案