在gridview中创建标题和子标题

时间:2015-01-20 05:33:40

标签: c# asp.net gridview

下面是我的输出,我试图合并标头和子标头。但是这对我来说不起作用,因为前三列正在重新创建,我希望标题脚本高于fromdate,todate,分析师

<tr>
    <td align="center">
        <asp:GridView ID="grv_taskfilter" runat="server" AutoGenerateColumns="False" RowStyle-Wrap="true" 
            Style="margin-top: 0px" BackColor="White" BorderColor="White" BorderStyle="Ridge" CaptionAlign="Bottom"
            BorderWidth="1px"   CellPadding="2" CellSpacing="1" GridLines="None" Font-Size="XX-Small" 
             OnRowCreated="grv_taskfilter_RowCreated"  >
            <Columns>

                <asp:TemplateField HeaderText="Project Name">
                   <ItemTemplate>
                        <a href="Task_description.aspx?id=<%#Eval("ProjectName")%>&flag=0"><%# Eval("ProjectName") %></a>                                      
                    </ItemTemplate>
                 </asp:TemplateField>
                  <asp:BoundField DataField="ProjectNos" HeaderText="Project nos" ItemStyle-HorizontalAlign="Left"  />
                 <asp:BoundField DataField="Questionnaire Submission for sripting" HeaderText="Questionnaire Submission for sripting" ItemStyle-HorizontalAlign="Left" />
                 <asp:BoundField DataField="SFromdate" HeaderText="FromDate scripting" ItemStyle-HorizontalAlign="Left" />
                <asp:BoundField DataField="SToDate" HeaderText="ToDate scripting" ItemStyle-HorizontalAlign="Left" />
                <asp:BoundField DataField="SAnalyst" HeaderText="Analyst scripting" ItemStyle-HorizontalAlign="Left" />



            </Columns>
              <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#FFCC00" Font-Bold="True" ForeColor="Black" HorizontalAlign="Center" />
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#0000A9" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#000065" />
        </asp:GridView>

    </td>
</tr>

上面提到的是设计器代码,请找到创建行标题的代码

protected void grv_taskfilter_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header) // If header created
    {
        GridView Projectgrid = (GridView)sender;

        // Creating a Row
        GridViewRow HeaderRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);


        TableCell HeaderCell = new TableCell();
        HeaderCell.Text = "ProjectName";
        HeaderCell.HorizontalAlign = HorizontalAlign.Center;
        HeaderCell.RowSpan = 2;
        HeaderCell.ColumnSpan = 1;
        HeaderCell.CssClass = "HeaderStyle";
        HeaderRow.Cells.Add(HeaderCell);


        HeaderCell = new TableCell();
        HeaderCell.Text = "Project nos";
        HeaderCell.HorizontalAlign = HorizontalAlign.Center;
        HeaderCell.RowSpan = 2;
        HeaderCell.ColumnSpan = 1;
        HeaderCell.CssClass = "HeaderStyle";
        HeaderRow.Cells.Add(HeaderCell);


        HeaderCell = new TableCell();
        HeaderCell.Text = "Questionnaire Submission for sripting";
        HeaderCell.HorizontalAlign = HorizontalAlign.Center;
        HeaderCell.RowSpan = 2;
        HeaderCell.ColumnSpan = 1;
        HeaderCell.CssClass = "HeaderStyle";
        HeaderRow.Cells.Add(HeaderCell);


        HeaderCell = new TableCell();
        HeaderCell.Text = "Scripting";
        HeaderCell.HorizontalAlign = HorizontalAlign.Center;
        HeaderCell.ColumnSpan = 3; 
        HeaderCell.CssClass = "HeaderStyle";
        HeaderRow.Cells.Add(HeaderCell);

        //Adding the Row at the 0th position (first row) in the Grid
        Projectgrid.Controls[0].Controls.AddAt(0, HeaderRow);
    }
}

enter image description here

2 个答案:

答案 0 :(得分:1)

根据您的要求,您需要标题脚本来自日期,todate,分析师

这些列

所以你可以通过将col col 6给头标题Scripting

来做到这一点
HeaderCell = new TableCell();
HeaderCell.Text = "Scripting";
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.ColumnSpan = 6; // Give Colspan 6 to header
HeaderCell.CssClass = "HeaderStyle";
HeaderRow.Cells.Add(HeaderCell);

答案 1 :(得分:1)

  

但是这对我来说不起作用,因为前三列正在增加   重新创建

因为你告诉它这样做。在网格视图中创建多行时,你所做的实际上是黑帮IMO的“一部分”。要完成解决方案,您需要隐藏您创建的三个额外标题。为此,您需要将gridview绑定到RowDataBound事件,如下所示:

protected void grv_taskfilter_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[0].Visible = false; 
        e.Row.Cells[1].Visible = false; 
        e.Row.Cells[2].Visible = false;  
    }
}