GridView编辑的最后一行失败

时间:2014-05-02 13:51:21

标签: asp.net vb.net gridview

我需要在Gridview中添加Sub-Total和Total。在为子总计和总行添加代码之后,在网格的最终数据行上,"编辑"按钮没有长期发生它的事件。其余的网格编辑事件都会触发。我有几个不同的"子部分和#34;

到目前为止我发现了什么:

Sub-Total行的数量与不触发Edit事件的行数相同。 (IE如果有2个子总计行,那么最后两个编辑按钮不再触发事件,仅导致回发删除小计总行。如果有3个则编辑不起作用,等等等))< / p>

如果没有Sub-Total行,Edit事件将按预期工作。

以下是我用来构建网格和Sub-Total / Total行的代码:

Row_DataBound:

Protected Sub gvFTEMedProc_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvFTEMedProc.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        If DataBinder.Eval(e.Row.DataItem, "F_Med_Proced") <> "FTE Health Room Visit" Then
            If medicationCount <= 9 Then
                medicationCount += 1
                medicationProcedureType = "Medication"
            Else
                medicationCount += 1
                medicationProcedureType = "Procedures"
            End If
            Dim dblMedProcCount As Double = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "F_Med_Proced_Num"))
            dblMedProcTotal += dblMedProcCount
            grandTotalMedProc += dblMedProcCount
        End If
    End If
    If e.Row.RowType = DataControlRowType.Footer Then
        e.Row.Cells(0).ColumnSpan = 2
        e.Row.Cells(0).Text = "Total (minus Health Room Visits):"
        e.Row.Cells(1).ColumnSpan = 2
        e.Row.Cells(1).Text = grandTotalMedProc.ToString("n0")
        e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Right
        e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center
        e.Row.Cells(2).Visible = False
        e.Row.Cells(3).Visible = False
        e.Row.Font.Bold = True
        medicationProcedureType = String.Empty
        medicationCount = 0
    End If
End Sub

Row_Created:

Protected Sub gvFTEMedProc_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvFTEMedProc.RowCreated
    Dim IsSubTotalRowNeeded As Boolean = False

    If (medicationProcedureType <> String.Empty) AndAlso (DataBinder.Eval(e.Row.DataItem, "F_Med_Proced") IsNot Nothing) Then
        If medicationCount = 9 Then 'DataBinder.Eval(e.Row.DataItem, "School_Type").ToString() Then
            IsSubTotalRowNeeded = True
        End If
    End If

    If (medicationProcedureType <> String.Empty) AndAlso (DataBinder.Eval(e.Row.DataItem, "F_Med_Proced") Is Nothing) Then
        IsSubTotalRowNeeded = True
        intSubTotalIndex = 0
    End If
    If medicationProcedureType = "Procedures" Then
        Dim rowIndex = e.Row.RowIndex
    End If
    'The Issue is happening when I add this IF Statement.
    If IsSubTotalRowNeeded Then
        Dim gvtest As GridView = DirectCast(sender, GridView)

        'Create a Row
        Dim SubTotalRow As New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert)

        'Adding Total Cell
        Dim HeaderCell As New TableCell()
        HeaderCell.Text = medicationProcedureType + " Sub Total:"
        HeaderCell.ColumnSpan = 2
        HeaderCell.HorizontalAlign = HorizontalAlign.Right
        HeaderCell.CssClass = "SubTotalRowStyle"
        SubTotalRow.Cells.Add(HeaderCell)

        'Adding Medication Column
        HeaderCell = New TableCell()
        HeaderCell.Text = String.Format("{0:N0}", dblMedProcTotal)
        HeaderCell.ColumnSpan = 2
        HeaderCell.HorizontalAlign = HorizontalAlign.Center
        HeaderCell.CssClass = "SubTotalRowStyle"
        SubTotalRow.Cells.Add(HeaderCell)

        'Adding the Row at the RowIndex position in the Grid
        gvtest.Controls(0).Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, SubTotalRow)
        intSubTotalIndex += 1

        dblMedProcTotal = 0
    End If
End Sub

ASP:

<asp:GridView ID="gvFTEMedProc" runat="server" CellPadding="4" ForeColor="#333333"
        Width="100%" AutoGenerateColumns="False" ShowFooter="True">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:HiddenField ID="hdf_HCID" runat="server" Value='<%# Bind("FTE_MP_ID") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="F_Med_Proced" HeaderText="FTE Medication Procedure" />
            <asp:BoundField DataField="F_Med_Proced_Num" HeaderText="FTE Medication Procedure Number"
                DataFormatString="{0:N0}" />
            <asp:TemplateField ItemStyle-Width="30px" HeaderText="Edit">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" OnClick="Edit"></asp:LinkButton>
                </ItemTemplate>
                <ItemStyle Width="30px" />
            </asp:TemplateField>
            <asp:TemplateField ItemStyle-Width="30px" HeaderText="Delete" Visible="false">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" OnClick="Delete"></asp:LinkButton>
                </ItemTemplate>
                <ItemStyle Width="30px" />
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>

1 个答案:

答案 0 :(得分:-1)

解决方案是将数据绑定添加到Page_Load中。 (但是,在我的情况下,每个page_load都需要“if not page.ispostback()”)

在我的情况下,我只是在从下拉菜单中选择时绑定数据。因此,当在回发上加载页面时,它不会重新绑定网格的Edit列(奇怪的是,只有网格的最后1-3行不会绑定,前10-12行将正常绑定)

在我的Change Event函数中,我处理了选择是否有效,如果进行了有效选择,则调用数据绑定函数。通过将更改事件功能添加到Page_Load,问题得以解决。现在,编辑按钮和网格在每个page_load上都可以正常运行,回传与否。