自定义Gridview的表头

    #region 自定义表头

    protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //判断创建的行是不是标题行
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection tcl = e.Row.Cells;
            //清除自动生成的表头
            tcl.Clear();

            //添加新的表头
            tcl.Add(new TableHeaderCell());
            tcl[0].RowSpan = 2;
            tcl[0].Text = "标题1";
            tcl[0].BackColor = (Color)ColorTranslator.FromHtml("#F3F9FF");
            tcl[0].BorderColor = (Color)ColorTranslator.FromHtml("#DAE9FF");

            //Label l1 = new Label();
            //l1.Text = "label";
            //Button bt = new Button();
            //bt.Text = "button";
            因为自定义表头,所以原来系统中实现的排序功能就失效了
            下面语句可以自己在表头中添加控件,自己实现排序功能
            //tcl[0].Controls.Add(l1);
            //tcl[0].Controls.Add(bt);

            tcl.Add(new TableHeaderCell());
            tcl[1].RowSpan = 2;
            tcl[1].Text = "标题2";
            tcl[1].BackColor = (Color)ColorTranslator.FromHtml("#F3F9FF");
            tcl[1].BorderColor = (Color)ColorTranslator.FromHtml("#DAE9FF");

            tcl.Add(new TableHeaderCell());
            tcl[2].ColumnSpan = 3;
            tcl[2].Text = "标题3";
            tcl[2].BackColor = (Color)ColorTranslator.FromHtml("#FEEFFF");
            tcl[2].BorderColor = (Color)ColorTranslator.FromHtml("#FCDAFF");

            tcl.Add(new TableHeaderCell());
            tcl[3].ColumnSpan = 3;
            tcl[3].Text = "标题4";
            tcl[3].BackColor = (Color)ColorTranslator.FromHtml("#EBFFFD");
            tcl[3].BorderColor = (Color)ColorTranslator.FromHtml("#CDFBF5");

            tcl.Add(new TableHeaderCell());
            tcl[4].ColumnSpan = 3;
            tcl[4].Text = "标题5";
            tcl[4].BackColor = (Color)ColorTranslator.FromHtml("#FBFFD3");
            tcl[4].BorderColor = (Color)ColorTranslator.FromHtml("#F9FFCB");

            tcl.Add(new TableHeaderCell());
            tcl[5].RowSpan = 2;
            tcl[5].Text = "标题6";
            tcl[5].BackColor = (Color)ColorTranslator.FromHtml("#F3F9FF");
            tcl[5].BorderColor = (Color)ColorTranslator.FromHtml("#DAE9FF");

            //这段是重点 其实在生成的html中tcl[6]转化为<th>标题</th>
            //所以依照该原则注入html标签来实现,原理有些类似于SQL注入攻击
            tcl.Add(new TableHeaderCell());
            tcl[6].RowSpan = 2;
            tcl[6].Text = "标题7</th></tr><tr><th style=/"background-color:#FEEFFF;border-color:#FCDAFF;/">标题3-1</th><th style=/"background-color:#FEEFFF;border-color:#FCDAFF;/">标题3-2</th><th style=/"background-color:#FEEFFF;border-color:#FCDAFF;/">标题3-3</th><th style=/"background-color:#EBFFFD;border-color:#CDFBF5;/">标题4-1</th><th style=/"background-color:#EBFFFD;border-color:#CDFBF5;/">标题4-2</th><th style=/"background-color:#EBFFFD;border-color:#CDFBF5;/">标题4-3</th><th style=/"background-color:#FBFFD3;border-color:#F9FFCB;/">标题5-1</th><th style=/"background-color:#FBFFD3;border-color:#F9FFCB;/">标题5-2</th><th style=/"background-color:#FBFFD3;border-color:#F9FFCB;/">标题5-3";
            tcl[6].BackColor = (Color)ColorTranslator.FromHtml("#F3F9FF");
            tcl[6].BorderColor = (Color)ColorTranslator.FromHtml("#DAE9FF");

        }


    }
    #endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingxyd_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值