.NET WebForm GridView 表头跨行跨列实现

本文介绍如何使用ASP.NET中的GridView控件自定义表头样式,包括行创建事件gvInfo_RowCreated的具体实现,以及如何设置表头单元格的文本、字体样式、跨行和跨列属性。

需要实现的效果

通过GridView的OnRowCreated事件实现 

        /// <summary>
        /// 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvInfo_RowCreated(object sender, GridViewRowEventArgs e)
        {
            DataControlRowType rowType = e.Row.RowType;
            if(rowType== DataControlRowType.Header)
            {
                //第一行表头
                TableCellCollection tcHeader = e.Row.Cells;
                tcHeader.Clear();

                tcHeader.Add(new TableHeaderCell());
                tcHeader[0].Text = "队组";
                tcHeader[0].Font.Bold = true;//加粗
                tcHeader[0].Attributes.Add("rowspan", "2"); //跨Row
                tcHeader[0].Attributes.Add("style", "width:150px;padding:20px 8px"); //样式设置

                tcHeader.Add(new TableHeaderCell());
                tcHeader[1].Text = "月计划";
                tcHeader[1].Font.Bold = true;//加粗
                tcHeader[1].Attributes.Add("rowspan", "2"); //跨Row
                tcHeader[1].Attributes.Add("style", "width:120px;padding:20px 8px"); //样式设置

                tcHeader.Add(new TableHeaderCell());
                tcHeader[2].Text = "当日";
                tcHeader[2].Font.Bold = true;//加粗
                tcHeader[2].Attributes.Add("colspan", "3"); //跨Column

                tcHeader.Add(new TableHeaderCell());                
                tcHeader[3].Text = "月累计</tr><tr style='background-color:WhiteSmoke;'>";//<tr>
                tcHeader[3].Font.Bold = true;//加粗
                tcHeader[3].Attributes.Add("colspan", "3"); //跨Column
                //第二行表头
                tcHeader.Add(new TableHeaderCell());
                tcHeader[4].Text = "计划";
                tcHeader[4].Font.Bold = true;//加粗
                tcHeader[4].Attributes.Add("style", "width:120px"); //宽度设置

                tcHeader.Add(new TableHeaderCell());
                tcHeader[5].Text = "实际";
                tcHeader[5].Font.Bold = true;//加粗
                tcHeader[5].Attributes.Add("style", "width:120px"); //宽度设置

                tcHeader.Add(new TableHeaderCell());
                tcHeader[6].Text = "超亏";
                tcHeader[6].Font.Bold = true;//加粗
                tcHeader[6].Attributes.Add("style", "width:120px"); //宽度设置

                tcHeader.Add(new TableHeaderCell());
                tcHeader[7].Text = "计划";
                tcHeader[7].Font.Bold = true;//加粗
                tcHeader[7].Attributes.Add("style", "width:120px"); //宽度设置

                tcHeader.Add(new TableHeaderCell());
                tcHeader[8].Text = "实际";
                tcHeader[8].Font.Bold = true;//加粗
                tcHeader[8].Attributes.Add("style", "width:120px"); //宽度设置

                tcHeader.Add(new TableHeaderCell());
                tcHeader[9].Text = "超亏";
                tcHeader[9].Font.Bold = true;//加粗
                tcHeader[9].Attributes.Add("style", "width:120px"); //宽度设置
            }
        }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值