如何添加页眉和副标题中的GridView [英] How to add Header and Subheader in Gridview

查看:134
本文介绍了如何添加页眉和副标题中的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下如何添加页眉和副标题在下面的图片!


解决方案

喜你可以做这样的

 <%@页面语言=C#AutoEventWireup =真codeFILE =Default.aspx.cs继承=_默认%GT;!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <标题>无标题页< /标题>
< /头>
<身体GT;
    <表ID =form1的=服务器>
    < D​​IV>
        < ASP:GridView控件ID =grvMergeHeader=服务器
                      背景色=浅金黄
                      BORDERCOLOR =长株潭边框宽度=5像素
                      CELLPADDING =3前景色=黑
                      网格线=无边框样式=无CELLSPACING =2
                      的AutoGenerateColumns =FALSE
                      的DataSourceID =SqlDataSource1
                      OnRowCreated =grvMergeHeader_RowCreated>
              < FooterStyle背景色=长株潭/>
              < SelectedRowStyle背景色=深青蓝前景色=GhostWhite/>
              < PagerStyle背景色=淡金黄前景色=深青蓝
                      Horizo​​ntalAlign =中心/>
              < HeaderStyle背景色=长株潭字体粗体=真/>
              < AlternatingRowStyle背景色=淡金黄/>
            <柱体和GT;
                < ASP:BoundField的数据字段=的DepartmentID
                                的HeaderText =的DepartmentID
                                SORTEX pression =的DepartmentID/>
                < ASP:BoundField的数据字段=部门
                                的HeaderText =部门
                                SORTEX pression =部门/>
                < ASP:BoundField的数据字段=姓名
                                的HeaderText =姓名
                                SORTEX pression =名称/>
                < ASP:BoundField的数据字段=位置
                                的HeaderText =位置
                                SORTEX pression =位置/>
            < /专栏>
        < / ASP:GridView的>
        < ASP:SqlDataSource的ID =SqlDataSource1=服务器的ConnectionString =<%$的ConnectionStrings:的ConnectionString%>中
            的SelectCommand =SELECT [的DepartmentID],[部门],[名],[位置] FROM [雇员]>
        < / ASP:SqlDataSource的>
        &安培; NBSP;< / DIV>
    < /表及GT;
< /身体GT;
< / HTML>

后面

code

 保护无效grvMergeHeader_RowCreated(对象发件人,GridViewRowEventArgs E)
    {
        如果(e.Row.RowType == DataControlRowType.Header)
        {
            GridView控件HeaderGrid =(GridView的)发件人;
            GridViewRow HeaderGridRow =新GridViewRow(0,0,DataControlRowType.Header,DataControlRowState.Insert);
            TableCell的HeaderCell =新的TableCell();
            HeaderCell.Text =部门;
            HeaderCell.ColumnSpan = 2;
            HeaderGridRow.Cells.Add(HeaderCell);            HeaderCell =新的TableCell();
            HeaderCell.Text =雇员;
            HeaderCell.ColumnSpan = 2;
            HeaderGridRow.Cells.Add(HeaderCell);            grvMergeHeader.Controls [0] .Controls.AddAt(0,HeaderGridRow);        }

Could Anyone explain How to add Header and Subheader in Gridview shown in the below picture!!

解决方案

hi you can do it like this

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grvMergeHeader" runat="server" 
                      BackColor="LightGoldenrodYellow" 
                      BorderColor="Tan" BorderWidth="5px" 
                      CellPadding="3" ForeColor="Black" 
                      GridLines="None" BorderStyle="None" CellSpacing="2" 
                      AutoGenerateColumns="False" 
                      DataSourceID="SqlDataSource1" 
                      OnRowCreated="grvMergeHeader_RowCreated">
              <FooterStyle BackColor="Tan" />
              <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
              <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 
                      HorizontalAlign="Center" />
              <HeaderStyle BackColor="Tan" Font-Bold="True" />
              <AlternatingRowStyle BackColor="PaleGoldenrod" />
            <Columns>
                <asp:BoundField DataField="DepartMentID" 
                                HeaderText="DepartMentID" 
                                SortExpression="DepartMentID" />
                <asp:BoundField DataField="DepartMent" 
                                HeaderText="DepartMent" 
                                SortExpression="DepartMent" />
                <asp:BoundField DataField="Name" 
                                HeaderText="Name" 
                                SortExpression="Name" />
                <asp:BoundField DataField="Location" 
                                HeaderText="Location" 
                                SortExpression="Location" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT [DepartMentID], [DepartMent], [Name], [Location] FROM [Employee]">
        </asp:SqlDataSource>
        &nbsp;</div>
    </form>
</body>
</html>

Code behind

protected void grvMergeHeader_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 = "Department";
            HeaderCell.ColumnSpan = 2;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Employee";
            HeaderCell.ColumnSpan = 2;
            HeaderGridRow.Cells.Add(HeaderCell);

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

        } 

这篇关于如何添加页眉和副标题中的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆