This question already has answers here:
How to add Header and Subheader in Gridview
                                
                                    (2个答案)
                                
                        
                                4年前关闭。
            
                    
我想在BoundField上方添加一行。这是我的GridView:

   <asp:GridView ID="grdvProductChurn" runat="server" CellPadding="4" HeaderStyle-BorderStyle="None"
    BorderColor="#666666" BorderStyle="Solid" AllowPaging="True" AutoGenerateColumns="false" PageSize="30" DataSourceID="DataSource_ProductChurn"
    AllowSorting="True" ForeColor="#666666" CellSpacing="1" DataFormatString="{0:###,###,###,###,###}"
    CaptionAlign="Left"  Width="960px" HeaderStyle-HorizontalAlign="Center" HorizontalAlign="Center" CssClass="GridView2"
    Height="119px"  >
    <Columns>



        <asp:BoundField ReadOnly="true" DataField="Produktgruppe" HeaderText="Produktgruppe" SortExpression="Produktgruppe" HeaderStyle-HorizontalAlign="Left" >
        <ControlStyle Font-Bold="False" />
        <ItemStyle HorizontalAlign="Left" />
        </asp:BoundField>

        <asp:BoundField ReadOnly="true" DataField="Produkt" HeaderText="Produkt" SortExpression="Produkt" HeaderStyle-HorizontalAlign="Left" >
        <ControlStyle Font-Bold="False" />
        <ItemStyle HorizontalAlign="Left" />
        </asp:BoundField>


        <asp:BoundField ReadOnly="true" DataField="Anzahl_Jan" HeaderText="Jan" SortExpression="Anzahl_Jan" HeaderStyle-HorizontalAlign="Center" DataFormatString="{0:###,###,###,###,###,0}" >
        <ControlStyle Font-Bold="False" />
        <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>

          <asp:BoundField ReadOnly="true" DataField="Anzahl_Feb" HeaderText="Feb" SortExpression="Anzahl_Feb" HeaderStyle-HorizontalAlign="Center" DataFormatString="{0:###,###,###,###,###,0}" >
        <ControlStyle Font-Bold="False" />
        <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>


        <asp:BoundField ReadOnly="true" DataField="Anzahl_Mar" HeaderText="Mar" SortExpression="Anzahl_Mar" HeaderStyle-HorizontalAlign="Center" DataFormatString="{0:###,###,###,###,###,0}" >
        <ControlStyle Font-Bold="False" />
        <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>

         <asp:BoundField ReadOnly="true" DataField="Anzahl_Apr" HeaderText="Apr" SortExpression="Anzahl_Apr" HeaderStyle-HorizontalAlign="Center" DataFormatString="{0:###,###,###,###,###,0}" >
        <ControlStyle Font-Bold="False" />
        <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>

         <asp:BoundField ReadOnly="true" DataField="Anzahl_May" HeaderText="May" SortExpression="Anzahl_May" HeaderStyle-HorizontalAlign="Center" DataFormatString="{0:###,###,###,###,###,0}" >
        <ControlStyle Font-Bold="False" />
        <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>

        <asp:BoundField ReadOnly="true" DataField="Anzahl_Jun" HeaderText="Jun" SortExpression="Anzahl_Jun" HeaderStyle-HorizontalAlign="Center" DataFormatString="{0:###,###,###,###,###,0}" >
        <ControlStyle Font-Bold="False" />
        <ItemStyle HorizontalAlign="Center" />
        </asp:BoundField>



    </Columns>


    <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" ForeColor="#666666" />
    <PagerSettings Mode="Numeric" />
    <PagerStyle ForeColor="#11AAFF" Font-Names='"Trebuchet MS", Arial, Sans' Font-Size="12px" HorizontalAlign="Left" />
    <RowStyle HorizontalAlign="Left" VerticalAlign="Top" />

    <SelectedRowStyle HorizontalAlign="Left"></SelectedRowStyle>
</asp:GridView>


目前看起来像这样,但是现在我想在上面添加一行:


它看起来应该像这样,如何在BoundField HeaderText上方添加一行?
如果可能的话,我想在aspx文件中执行此操作。

最佳答案

我在背后的代码中做了一个方法。
这是我的代码,现在可以正常工作。

protected void grdvProductChurn_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        GridView HeaderGrid = (GridView)sender;
        GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
        TableCell HeaderCell = new TableCell();
        HeaderCell.Text = "";
        HeaderCell.ColumnSpan = 2;
        HeaderGridRow.Cells.Add(HeaderCell);

        HeaderCell = new TableCell();
        HeaderCell.Text = "Anzahl";
        HeaderCell.ColumnSpan = 6;
        HeaderGridRow.Cells.Add(HeaderCell);

        grdvProductChurn.Controls[0].Controls.AddAt(0, HeaderGridRow);

    }
}

关于c# - 如何在ASP.NET GridView中的BoundField上方添加一行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31267922/

10-13 23:39