none
階層式gridview運算的應用問題 RRS feed

  • 問題

  • 請教各位前輩,這是兩層gridview的運算問題,主要是要計算裡層gridview2的出席率百分比的部份
    出席率=(正常報名簽到課程數)/user有報名的全部課程

    而我目前語法如下
    count.aspx
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server"></asp:Label><br />
                        <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("MemberId") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView2_RowDataBound" ShowFooter="True">
                            <Columns>
                                <asp:BoundField DataField="ActiveName" HeaderText="課程名稱">
                                    <ItemStyle HorizontalAlign="Left" />
                                    <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="AttendWay" HeaderText="報名方式">
                                    <ItemStyle HorizontalAlign="Left" />
                                    <HeaderStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="SignIn" HeaderText="正常報名與否" />
                            </Columns>
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

     

    count.aspx.vb
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            Dim dt3 As New DataTable
            Select Case e.Row.RowType
                Case DataControlRowType.Header
                Case DataControlRowType.DataRow
                    If e.Row.RowType = DataControlRowType.DataRow Then
                        CType(e.Row.Cells(0).FindControl("Label1"), Label).Text = e.Row.DataItemIndex + 1
                        Dim g As GridView = e.Row.Cells(4).FindControl("gridview2")
                        Dim h As HiddenField = e.Row.Cells(0).FindControl("HiddenField1")
                        Dim l As Label = e.Row.Cells(5).FindControl("Label2")
                        dt3=sit.all.web(".........")
                        With g
                            .DataSource = dt3
                            .DataBind()
                        End With
                        '計算出席率
                        dt4 = SIT.AllDT.WEB("SELECT count(*) as total FROM ePassport_ToSignUp INNER JOIN ePassport_AttendWay ON ePassport_ToSignUp.AttendWayId = ePassport_AttendWay.AttendWayId INNER JOIN ePassport_Active ON ePassport_ToSignUp.ActiveId = ePassport_Active.ActiveId WHERE (Id='" & h.Value & "')")
                        If dt4.Rows.Count <> 0 Then
                            ViewState("total") = dt4.Rows(0)("total")
                        End If
                    End If
            End Select

        End Sub

        Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
            Select Case e.Row.RowType
                Case DataControlRowType.Header
                    FCON = 0 : ViewState("total") = 0
                Case DataControlRowType.DataRow
                    If e.Row.Cells(2).Text = "O" Then
                        FCON += 1
                    End If
                Case DataControlRowType.Footer
                    e.Row.Cells(2).Text = Math.Round((FCON / ViewState("total")) * 100, 2)
                    e.Row.ForeColor = Drawing.Color.Red
                    e.Row.Font.Bold = True
            End Select
        End Sub

     

    由於要計算每位user報名課程的總數,我將它的值篩給ViewState("total"),再將它的值給裡層的gridview2來使用,可是發現算出來的出席率(e.row.cells(2).text)卻都不對,而我驗證過FCON(有正常報名簽到)的次數是正確的,是不是沒辦法把viewstate("total")給裡層gridview2來使用呢?是否有其它的方式可替代
    謝謝

    2008年1月7日 下午 05:07

所有回覆

  • 你在取得 ViewState("total") 的值之前,就已經先將 GridView2 做了一次 DataBind() 的動作了,在 GridView2_RowDataBound 當然找不到 ViewState("total") 的資料阿!!

     

    這是程式邏輯的問題,你的寫法應該沒問題。


    ----
    From: Will
    Blog: http://blog.miniasp.com/
    記載著 Will 在網路世界的學習心得與技術分享

    2008年1月7日 下午 05:17