簡體   English   中英

使GridView頁腳數據像行數據一樣綁定

[英]Making a GridView footer data bound like a Row data

我有一個使用C#創建的使用GridViews的ASP.NET項目。 頁腳行包含基於列數據的總數與表示該數據應該有多少的數據中的設置值的比率。 用戶需要能夠修改第二個值並更新頁腳。 我不知道如何做到這一點,甚至不可能。 我打算在下面使用另一個GridView,但是確保列線在兩者之間同步是一場噩夢。 有任何想法嗎?

另外,當我更改列數據(使用“編輯/更新”行)時,單擊“更新”時不會更新總數,而再次單擊“編輯”時不會更新總數。 誰能告訴我這是為什么,以及如何更新頁腳中的總數?

如果您發現編輯后的字段在“單擊后刷新”狀態下刷新,通常是由於認為您已使GridView反彈但實際上沒有反彈或您在進行更新之前已經反彈。

使用Gridview時,每當您在頁腳中放置字段時,一種典型的情況是在頁面生命周期的數據綁定操作期間計算其值

就像這樣,您的.aspx文件定義了一些頁腳字段,通常是一些BoundField ,這些字段已被轉換為TemplateField 這是一個片段:

<asp:GridView ID="GridView1" runat="server" ShowFooter="true" ...>
  <Columns>
    <asp:BoundField DataField="Field1" HeaderText="Title" SortExpression="Field1" />
    <asp:TemplateField>
      <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Field2") %>' ></asp:Label>
      </ItemTemplate>
      <FooterTemplate>
        <asp:Label ID="FooterLabel1" runat="server" Text="" ></asp:Label>
      </FooterTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

后面代碼中的GridView事件:通常,這是您需要根據更改的行值來填充頁腳字段的步驟。 這是VB,但您應該能夠輕松地對其進行轉換。

// Create a variable at the Page level that will exist for 
// the duration of the Page LifeCycle
Private FooterLabel1SubTotal as Double

// Initialize
Private Sub GridView1_DataBinding(sender As Object, e As EventArgs) Handles GridView1.DataBinding
  FooterLabel1SubTotal = 0.0
End Sub

// Accumulate
Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
  If e.Row.RowType = DataRow Then
    Dim Field2 as Label = e.Row.FindControl("Field2")
    FooterLabel1SubTotal += Convert.ToDouble(Field2.Text)
  End If
End Sub

// Populate Footer with formated value
Private Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound
  FooterLabel1 = GridView1.FooterRow.FindControl("FooterLabel1")
  FooterLabel1.Text = String.Format("{0:F2}", FooterLabel1SubTotal)

End Sub

現在,如果您使用GridView的任何“內置編輯”功能,這將導致回發並導致GridView重新綁定,這應每次重新計算頁腳字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM