如何在ASP.NET GridView中的BoundField上添加一行

时间:2015-07-07 11:56:28

标签: 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

3 个答案:

答案 0 :(得分:3)

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

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);

    }
}

答案 1 :(得分:1)

您可以尝试使用此示例:

protected void HeaderTest(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 = "Test";
            HeaderCell.ColumnSpan = 6;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Test1";
            HeaderCell.ColumnSpan = 4;
            HeaderGridRow.Cells.Add(HeaderCell);

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

        }
    }

来自:How to add Header and Subheader in Gridview

答案 2 :(得分:0)

您可以添加如下 -

protected void Page_Load(object sender, EventArgs e)
{
   DataTable dt = new DataTable();
   DataColumn dc = new DataColumn();

   if (dt.Columns.Count == 0)
   {
       dt.Columns.Add("Produktgruppe", typeof(string));
       dt.Columns.Add("Produkt", typeof(string));
       dt.Columns.Add("Jan", typeof(string));
       dt.Columns.Add("Feb", typeof(string));
       dt.Columns.Add("March", typeof(string));
       dt.Columns.Add("may", typeof(string));
       dt.Columns.Add("Jun", typeof(string));
   }

   DataRow NewRow = dt.NewRow();
   NewRow[0] = " ";
   NewRow[1] = "Anzahl";
   dt.Rows.Add(NewRow); 
   GridView1.DataSource = dt;
   GridViewl.DataBind();
}

我希望这可以帮到你。