合并页脚行不正常

时间:2014-04-16 05:51:54

标签: c# asp.net vb.net gridview

我有13列的gridview。我想在页脚行中显示Grand Total预算。我想将单元格合并在一起,以便页脚行  显示得当。以下是我的代码

grdBasicBudget.FooterRow.Cells(0).ColumnSpan = 7
    grdBasicBudget.FooterRow.Cells.RemoveAt(1)
    grdBasicBudget.FooterRow.Cells.RemoveAt(2)
    grdBasicBudget.FooterRow.Cells.RemoveAt(3)
    grdBasicBudget.FooterRow.Cells.RemoveAt(4)
    grdBasicBudget.FooterRow.Cells.RemoveAt(5)
    grdBasicBudget.FooterRow.Cells.RemoveAt(6)

虽然有13列,但如果删除第7列,我会得到一个超出范围异常的索引 我想在页脚行显示这个

grdBasicBudget.FooterRow.Cells(0).Text = " GRAND TOTAL BUDGET :" + GrandTotBudget.ToString()

我的gridview看起来像这样

 <asp:GridView ID="grdBasicBudget" runat="server" AutoGenerateColumns="false" ShowFooter="true"
        Width="1500px" Style="text-align: right; vertical-align: middle" HorizontalAlign="Center"
        HeaderStyle-BackColor="#ffffcc">
        <RowStyle HorizontalAlign="Right" />
        <Columns>
            <asp:BoundField HeaderText="Part" DataField="part" />
            <asp:BoundField HeaderText="Revisi" DataField="revisit" />
            <asp:BoundField HeaderText="Revision Date(m/d/y)" DataField="revision_date" />
            <asp:BoundField HeaderText="Year" DataField="year" />
            <asp:BoundField HeaderText="TYPE" DataField="type" />
            <asp:BoundField HeaderText="Task1" DataField="task" />
            <asp:BoundField HeaderText="Task2" DataField="task2" />
            <asp:BoundField HeaderText="Activity" DataField="activity" />
            <asp:BoundField HeaderText="Contractor" DataField="contract" />
            <asp:BoundField HeaderText="Tie" DataField="tie" />
            <asp:BoundField HeaderText="Planned_Amount($)" DataField="planned_amount" />

            <asp:BoundField HeaderText="Estimated Delivery Date" DataField="estimated_del_date" />
            <asp:BoundField HeaderText="Notes" DataField="notes" />
        </Columns>
    </asp:GridView>

我想在计划金额单元格下方显示计算金额,并在金额后面显示总计预算总额,所以我想要这样的内容:

grdBasicBudget.FooterRow.Cells(0).Text = " GRAND TOTAL BUDGET" + GrandTotBudget.ToString()

GrandTotBudget部分应始终显示在计划金额单元格下方。

我也试过用这种方式编写代码:

Protected Sub grdBasicBudget_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdBasicBudget.RowDataBound
    Dim counter As Integer

    If e.Row.RowType = DataControlRowType.Footer Then
        For counter = 1 To grdBasicBudget.Columns.Count - 1
            e.Row.Cells.RemoveAt(counter)
        Next

        e.Row.Cells(0).ColumnSpan = grdBasicBudget.Columns.Count - 1
        e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Center
        e.Row.Cells(0).Text = "This is a test"

    End If
End Sub

仍然在第7列停止计数器。我知道我的gridview有超过7列。我不知道为什么我在第7栏上说错了  参数超出范围Exception未被用户代码处理。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试以下

For i As Integer = grdBasicBudget.Columns.Count - 1 To 1 Step -1
    grdBasicBudget.FooterRow.Cells.RemoveAt(i)
Next