c# - 如何在 ASP.NET GridView 中的 BoundField 上方添加一行

标签 c# asp.net gridview

<分区>

我想在 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>

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

应该是这样的,如何在 BoundField HeaderText 上面添加一行? 如果可能的话,我想在 aspx 文件中直接执行此操作。 enter image description here

最佳答案

我在代码隐藏中创建了一个方法。 现在这是我的代码,它可以工作。

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/

相关文章:

asp.net - DebugDiag 显示长时间运行

c# - 为什么我被迫编写行编辑事件?

c# - 意外的字符串总和

c# - ActionResult 返回后会发生什么?

asp.net - MSDN 中切换 View

c# - 启用 Unity 以解决来自 OwinContext 的依赖项

listview - WPF ListView GridView 复制到剪贴板

gridview - 不同列的条件格式

c# - 如何使用 oracle 11g 从 c# 返回数据

c# - 尝试访问 ASP.NET Web App 项目上的本地主机时出现 ERR_CONNECTION_RESET