ASP.Net循环通过Gridview添加Group Row后

时间:2017-01-12 01:19:43

标签: asp.net gridview

在GridView中,我将分组行添加到RowDataBound中的基础表,但是当我在ApplyChanges中循环并遍历每个GridViewRow时,并不是所有行都可以测试TextBox值等,行数减少了分组数量插入的行。虽然它确实遍历我忽略的分组行,但是错过了最后n行。

Private tmpSiteName As String = ""

Private Sub gvVehicles_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvVehicles.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
        If tmpSiteName <> drv("lcname").ToString() Then
            tmpSiteName = drv("lcname").ToString()

            Dim tbl As Table = TryCast(e.Row.Parent, Table)
            If tbl IsNot Nothing Then
                Dim row As New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
                Dim cell As New TableCell()

                ' Span the row across all of the columns in the Gridview
                cell.ColumnSpan = Me.gvVehicles.Columns.Count

                cell.Width = Unit.Percentage(100)
                'cell.Style.Add("font-weight", "bold")
                cell.Style.Add("background-color", "#f5f5f5")
                cell.Style.Add("color", "#000000")

                Dim span As New HtmlGenericControl("span")
                span.InnerHtml = tmpSiteName

                cell.Controls.Add(span)
                row.Cells.Add(cell)

                tbl.Rows.AddAt(tbl.Rows.Count - 1, row)
            End If
        End If
    End If
End Sub

Private Sub ApplyChanges()
    Dim AuctionDB As fleet.AuctionDB = New fleet.AuctionDB
    Dim OldFleetNo As String = ""
    Dim NewFleetNo As String = ""
    Dim OldReserve As Integer = 0
    Dim NewReserve As Integer = 0
    Dim MTA As Long = 0
    Dim Sitekey As String = ""
    Dim vcaudit As Long = 0

    Dim i As Integer = gvVehicles.Rows.Count

    Try
        For Each DRow As GridViewRow In gvVehicles.Rows

            Sitekey = TryCast(DRow.FindControl("hiddenSitekey"), HiddenField).Value
            MTA = Val(TryCast(DRow.FindControl("hiddenMTA"), HiddenField).Value)

            strChange = ""

            ' The Grouping rows throw up funny results, but we can catch MTA = 0
            If MTA > 0 Then
                OldFleetNo = TryCast(DRow.FindControl("hiddenFleetNo"), HiddenField).Value
                NewFleetNo = TryCast(DRow.FindControl("txtFleetNo"), TextBox).Text
                If NewFleetNo <> OldFleetNo Then
                    vcaudit = AuctionDB.FleetUpdates(Sitekey, MTA, "FLEET NO", OldReserve, NewFleetNo, Session("user_name"))
                    updateCount += 1
                    strChange = "Fleet No. changed from " & OldFleetNo & " to " & NewFleetNo & "<br /><br />"
                End If
            End If
        Next DRow

    Catch ex As Exception
        ShowMessage("Error: " & ex.Message, "alert-danger")
    End Try
End Sub

0 个答案:

没有答案