Answered by:
Convert from Gridview code to Repeater Code

Question
-
User151513110 posted
Hi,
I have using Repeater control in my project and I will place subtotal for each group data (add new row) . I tried to search SubTotal in repeater control . but still I not yet get the code, eventually Gridview control we can do it. however I have using repeater in my project. hence if its possible to convert gridview code to repeater code .
Protected Sub OnRowCreated(sender As Object, e As GridViewRowEventArgs) subTotal = 0 If e.Row.RowType = DataControlRowType.DataRow Then Dim dt As DataTable = TryCast(e.Row.DataItem, DataRowView).DataView.Table Dim orderId As Integer = Convert.ToInt32(dt.Rows(e.Row.RowIndex)("OrderID")) total += Convert.ToDecimal(dt.Rows(e.Row.RowIndex)("Price")) If orderId <> currentId Then If e.Row.RowIndex > 0 Then For i As Integer = subTotalRowIndex To e.Row.RowIndex - 1 subTotal += Convert.ToDecimal(GridView1.Rows(i).Cells(2).Text) Next Me.AddTotalRow("Sub Total", subTotal.ToString("N2")) subTotalRowIndex = e.Row.RowIndex End If currentId = orderId End If End If End Sub Private Sub AddTotalRow(labelText As String, value As String) Dim row As New GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal) row.BackColor = ColorTranslator.FromHtml("#F9F9F9") 'Empty Cell row.Cells.AddRange(New TableCell(2) {New TableCell(), New TableCell() With { _ .Text = labelText, _ .HorizontalAlign = HorizontalAlign.Right _ }, New TableCell() With { _ .Text = value, _ .HorizontalAlign = HorizontalAlign.Right _ }}) GridView1.Controls(0).Controls.Add(row) End Sub
I would expecting the results in repeater control..... Protected Sub ItemCreated(sender As Object, e As RepeaterItemEventArgs) End Sub Private Sub AddTotalRow(labelText As String, value As String) End Sub Any one please suggest me.
Tuesday, June 14, 2016 3:03 AM
Answers
-
User1724605321 posted
Hi Ravi_D,
Then read below articles with code sample about Grouping and Subtotaling Repeater for ASP.NET :
http://www.drdobbs.com/windows/a-grouping-and-subtotaling-repeater-for/219500455
http://www.aspsnippets.com/Articles/Display-SubTotal-and-Grand-Total-in-ASPNet-GridView.aspx
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 14, 2016 7:12 AM
All replies
-
User1724605321 posted
Hi Ravi_D,
You could add a FooterTemplate to show the total value , so you needn't create/add a row to the gridview/repeater . Code below is for your reference :
repeater template:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnDataBinding="Repeater1_DataBinding" OnItemDataBound="Repeater1_ItemDataBound"> <HeaderTemplate> <br /> </HeaderTemplate> <ItemTemplate> <br /> <%# Eval("ProductName") %>, <%# Eval("UnitPrice","{0:C}") %> </ItemTemplate> <FooterTemplate> <hr size="1" width="100%" /> <asp:Label ID="lblItemCount" runat="server" Text=""></asp:Label> <asp:Label ID="lblItemPrice" runat="server" Text=""></asp:Label> </FooterTemplate> </asp:Repeater>
Then you could define an member variable in Page class which can be used to store the sum valueduring the ItemDataBound event of Repeater :
Below is some code samples :
http://www.dotnetspider.com/forum/40213-How-to-add-sub-total-to-footer-template-of-repeat.aspx
https://bytes.com/topic/asp-net/answers/429670-repeater-control-footer-summary-asp-net-2-vb
Best Regards,
Nan Yu
Tuesday, June 14, 2016 6:30 AM -
User151513110 posted
Hi Nan Yu,
I would expecting below the results:
Tuesday, June 14, 2016 6:41 AM -
User1724605321 posted
Hi Ravi_D,
Then read below articles with code sample about Grouping and Subtotaling Repeater for ASP.NET :
http://www.drdobbs.com/windows/a-grouping-and-subtotaling-repeater-for/219500455
http://www.aspsnippets.com/Articles/Display-SubTotal-and-Grand-Total-in-ASPNet-GridView.aspx
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 14, 2016 7:12 AM