使用RenderMethod 委托实现DataGrid表头合并

文章介绍看到用RenderMethod委托可实现DataGrid表头合并,经测试效果不错。将DataGrid表头转化为用Table控制,能更灵活操作。作者还分享了相关代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天看到一篇文章,提出使用RenderMethod委托可以实现DataGrid表头的合并。自己测试了一下,效果不错,把DataGrid的表头转化为用Table来控制,就可以随心所欲了。可能前面有人用这种方法是实现过,但是我还是把代码贴上来,大家分享一下13.gif

  1None.gifusing System;
  2None.gifusing System.Collections;
  3None.gifusing System.ComponentModel;
  4None.gifusing System.Data;
  5None.gifusing System.Drawing;
  6None.gifusing System.Web;
  7None.gifusing System.Web.SessionState;
  8None.gifusing System.Web.UI;
  9None.gifusing System.Web.UI.WebControls;
 10None.gifusing System.Web.UI.HtmlControls;
 11None.gifusing System.Data.SqlClient;
 12None.gif
 13None.gifnamespace WebDataGridHeader
 14ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 16InBlock.gif    ///DataGrid表头合并问题
 17ExpandedSubBlockEnd.gif    /// </summary>

 18InBlock.gif    public class WebForm1 : System.Web.UI.Page
 19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 20InBlock.gif        protected System.Web.UI.WebControls.DataGrid DataGrid1;
 21InBlock.gif        protected System.Web.UI.WebControls.Label Label1;
 22InBlock.gif    
 23InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 25InBlock.gif            // 在此处放置用户代码以初始化页面
 26InBlock.gif            string m_strConn = "server=.;uid=sa;pwd=sa;database=Northwind";
 27InBlock.gif            SqlConnection conn = new SqlConnection(m_strConn);
 28InBlock.gif            
 29InBlock.gif            try
 30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 31InBlock.gif                conn.Open();
 32InBlock.gif
 33InBlock.gif                SqlCommand cmd = new SqlCommand("SELECT * FROM Employees",conn);
 34InBlock.gif            
 35InBlock.gif                SqlDataAdapter adp = new SqlDataAdapter(cmd);
 36InBlock.gif
 37InBlock.gif                DataTable dt = new DataTable();
 38InBlock.gif                adp.Fill(dt);
 39InBlock.gif
 40InBlock.gif                this.DataGrid1.DataSource = dt;
 41InBlock.gif                this.DataGrid1.DataBind();
 42ExpandedSubBlockEnd.gif            }

 43InBlock.gif            catch(Exception ex)
 44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 45InBlock.gif                throw ex;
 46ExpandedSubBlockEnd.gif            }

 47InBlock.gif            finally
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 49InBlock.gif                conn.Close();
 50ExpandedSubBlockEnd.gif            }

 51ExpandedSubBlockEnd.gif        }

 52InBlock.gif
 53ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
 54InBlock.gif        override protected void OnInit(EventArgs e)
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 56InBlock.gif            //
 57InBlock.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
 58InBlock.gif            //
 59InBlock.gif            InitializeComponent();
 60InBlock.gif            base.OnInit(e);
 61ExpandedSubBlockEnd.gif        }

 62InBlock.gif        
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 64InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 65InBlock.gif        /// 此方法的内容。
 66ExpandedSubBlockEnd.gif        /// </summary>

 67InBlock.gif        private void InitializeComponent()
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
 69InBlock.gif            this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
 70InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
 71InBlock.gif
 72ExpandedSubBlockEnd.gif        }

 73ExpandedSubBlockEnd.gif        #endregion

 74InBlock.gif        
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 76InBlock.gif        /// 创建Item
 77InBlock.gif        /// </summary>
 78InBlock.gif        /// <param name="sender"></param>
 79ExpandedSubBlockEnd.gif        /// <param name="e"></param>

 80InBlock.gif        private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 82InBlock.gif            //将Item的呈现方法定向到自定义的呈现方法上
 83InBlock.gif            ListItemType lit = e.Item.ItemType;
 84InBlock.gif            if(ListItemType.Header == lit)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 86InBlock.gif                e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));
 87ExpandedSubBlockEnd.gif            }

 88ExpandedSubBlockEnd.gif        }

 89InBlock.gif        
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 91InBlock.gif        /// 自定义的Item呈现方法
 92InBlock.gif        /// </summary>
 93InBlock.gif        /// <param name="writer"></param>
 94ExpandedSubBlockEnd.gif        /// <param name="ctl"></param>

 95InBlock.gif        private void NewRenderMethod(HtmlTextWriter writer,Control ctl)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 97InBlock.gif            //不需要从<TR>标签开始
 98InBlock.gif            //输出“联系电话”列
 99InBlock.gif            writer.Write("<TD colspan=\"3\" align=\"center\">联系电话</TD>\n");
100InBlock.gif
101InBlock.gif            //“地址”列必须有rowspan属性且必须在第一列呈现
102InBlock.gif            TableCell cell = (TableCell)ctl.Controls[ctl.Controls.Count - 1];
103InBlock.gif            cell.Attributes.Add("rowspan","2");
104InBlock.gif            cell.RenderControl(writer);
105InBlock.gif
106InBlock.gif            //现在关闭第一行
107InBlock.gif            writer.Write("</TR>\n");
108InBlock.gif
109InBlock.gif            //将设计时的样式属性添加到第二行使得两行的外观相似
110InBlock.gif            this.DataGrid1.HeaderStyle.AddAttributesToRender(writer);
111InBlock.gif
112InBlock.gif            //插入第二行
113InBlock.gif            writer.RenderBeginTag("TR");
114InBlock.gif
115InBlock.gif            //呈现除了最后一列(刚才已经呈现过了)外的所有在设计时定义的cells
116InBlock.gif            for(int i=0;i<=ctl.Controls.Count-2;i++)
117ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
118InBlock.gif                ctl.Controls[i].RenderControl(writer);
119ExpandedSubBlockEnd.gif            }

120InBlock.gif
121InBlock.gif            //不需要以</TR>结束
122ExpandedSubBlockEnd.gif        }

123ExpandedSubBlockEnd.gif    }

124ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值