在行之间插入标题如果Gridview中的名称不同

时间:2016-08-05 10:24:17

标签: c# asp.net gridview

我有一个gridview,我想用标题" AccountManager"(第1列)作为行之间的名称显示标题/空格。如果名称不同并且列的总和" Mins" ,"金额"和"利润"每个帐户管理员在空间/标题上方的每个" AccountManager"。

这是我的GridView(HTML):

<asp:gridview runat="server" id="GridView2" showfooter="true" 
    autogeneratecolumns="false" GridLines="None" CssClass="table" 
    HeaderStyle-CssClass="th" RowStyle-CssClass="td" Width="100%" OnRowDataBound="GridView2_RowDataBound" onrowcreated="GridView2_RowCreated">
    <columns>

        <asp:boundfield datafield="Date" headertext="Date" 
            footerstyle-font-bold="true"  >
<FooterStyle Font-Bold="True"></FooterStyle>
        </asp:boundfield>
        <asp:boundfield datafield="AccountManager" headertext="AccountManager" 
            footerstyle-font-bold="true"  >
<FooterStyle Font-Bold="True"></FooterStyle>
        </asp:boundfield>

         <asp:boundfield datafield="" headertext="Total" footerstyle-font-bold="true" 
            footertext="Grand Total:" >
<FooterStyle Font-Bold="True"></FooterStyle>
        </asp:boundfield>
        <asp:boundfield datafield="MIns" headertext="Mins" 
            footerstyle-font-bold="true"   >
<FooterStyle Font-Bold="True"></FooterStyle>
        </asp:boundfield>
        <asp:boundfield datafield="Amount" headertext="Amount" footerstyle-font-bold="true" 
             >
 <FooterStyle Font-Bold="True"></FooterStyle>
        </asp:boundfield>

        <asp:boundfield datafield="Profit" headertext="Profit"  
            footerstyle-font-bold="true">
<FooterStyle Font-Bold="True"></FooterStyle>
        </asp:boundfield>

   </columns>

 <HeaderStyle BackColor="#CEFF99" ForeColor="Black" BorderColor="#C1FF80" BorderStyle="Solid" 
              BorderWidth="1px"></HeaderStyle>

<RowStyle CssClass="td"></RowStyle>
</asp:gridview>

请帮助......我太麻烦了。

1 个答案:

答案 0 :(得分:0)

要对页脚中的值求和,可以使用此技术:

Displaying Total in Footer of GridView and also Add Sum of columns(row vise) in last Column

不显示重复列,如下所示(taken from Experts Exchange):

Protected Sub ResultGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles ResultGridView.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            'switch for first row 
            For j As Integer = 0 To 4
                If e.Row.RowIndex = 1 Then
                    Dim Gprev As GridViewRow = ResultGridView.Rows(e.Row.RowIndex - 1)

                    If Gprev.Cells(j).Text.Equals(e.Row.Cells(j).Text) Then
                        e.Row.Cells(j).Text = ""
                    End If
                End If

                'now continue with the rest of the rows 
                If e.Row.RowIndex > 1 Then
                    'set reference to the row index and the current value 
                    Dim intC As Integer = e.Row.RowIndex
                    Dim lookfor As String = e.Row.Cells(j).Text

                    'now loop back through checking previous entries for matches 
                    Do
                        Dim Gprev As GridViewRow = ResultGridView.Rows(intC - 1)

                        If Gprev.Cells(j).Text.Equals(e.Row.Cells(j).Text) Then
                            e.Row.Cells(j).Text = ""
                        End If

                        intC = intC - 1
                    Loop While intC > 0

                End If
            Next
        End If

    End Sub