1

I want to add a row above the BoundField. This is my 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>

At the moment it looks like that, but now I want to add a row above: enter image description here

It should look like that, how can I add a row above the BoundField HeaderText? I would like to do it right in the aspx file if it's even possible. enter image description here

0

3 Answers 3

3

I made a method inside the codebehind. This is my code now and it works.

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

You can try it with this example :

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

        }
    }

From : How to add Header and Subheader in Gridview

0

You can add it like following-

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

I hope this can help you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.