利用样式给DataGrid添加表头固定和列固定

昨天用添加模板列的方法练了个动态添加列,以适应DataGrid列根据数据库变化。用SetRenderMethodDelegate实现表头合并效果。今天看业务实在看不懂,没事之下,找了个模式,实现了表头和列的固定。不用只能用在IE5以上。
ContractedBlock.gif ExpandedBlockStart.gif 模式如下:
 1ExpandedBlockStart.gifContractedBlock.gif/**//* Div container to wrap the datagrid */
 2ExpandedBlockStart.gifContractedBlock.gifdiv#div-datagrid dot.gif{
 3InBlock.gifwidth: 420px;
 4InBlock.gifheight: 200px;
 5InBlock.gifoverflow: auto;
 6InBlock.gifscrollbar-base-color:#ffeaff;
 7ExpandedBlockEnd.gif}

 8None.gif
 9ExpandedBlockStart.gifContractedBlock.gif/**//* Locks the left column */
10ExpandedBlockStart.gifContractedBlock.giftd.locked, th.locked dot.gif{
11InBlock.giffont-size: 14px;
12InBlock.giffont-weight: bold;
13InBlock.giftext-align: center;
14InBlock.gifbackground-color: navy;
15InBlock.gifcolor: white;
16InBlock.gifborder-right: 1px solid silver;
17InBlock.gifposition:relative;
18InBlock.gifcursor: default
19ExpandedSubBlockStart.gifContractedSubBlock.gifleft: expression(document.getElementById("div-datagrid").scrollLeft-2); /**//*IE5+ only*/
20ExpandedBlockEnd.gif}
    
21None.gif
22ExpandedBlockStart.gifContractedBlock.gif/**//* Locks table header */
23ExpandedBlockStart.gifContractedBlock.gifth dot.gif{
24InBlock.giffont-size: 14px;
25InBlock.giffont-weight: bold;
26InBlock.giftext-align: center;
27InBlock.gifbackground-color: navy;
28InBlock.gifcolor: white;
29InBlock.gifborder-right: 1px solid silver;
30InBlock.gifposition:relative;
31InBlock.gifcursor: default
32ExpandedSubBlockStart.gifContractedSubBlock.giftop: expression(document.getElementById("div-datagrid").scrollTop-2); /**//*IE5+ only*/
33InBlock.gifz-index: 10;
34ExpandedBlockEnd.gif}

35None.gif
36ExpandedBlockStart.gifContractedBlock.gif/**//* Keeps the header as the top most item. Important for top left item*/
37ExpandedBlockStart.gifContractedBlock.gifth.locked dot.gif{z-index: 99;}
38None.gif
39ExpandedBlockStart.gifContractedBlock.gif/**//* DataGrid Item and AlternatingItem Style*/
40ExpandedBlockStart.gifContractedBlock.gif.GridRow dot.gif{font-size: 10pt; color: black; font-family: Arial; background-color:#ffffff; height:35px;}
41ExpandedBlockStart.gifContractedBlock.gif.GridAltRow dot.gif{font-size: 10pt; color: black; font-family: Arial; background-color:#eeeeee; height:35px;}
ContractedBlock.gif ExpandedBlockStart.gif asp.net Html
 1None.gif<%@Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Test1.WebForm1" %>
 2None.gif<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 3None.gif<HTML>
 4None.gif    <HEAD>
 5None.gif        <title>WebForm1</title>
 6None.gif        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
 7None.gif        <meta content="C#" name="CODE_LANGUAGE">
 8None.gif        <meta content="JavaScript" name="vs_defaultClientScript">
 9None.gif        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
10None.gif        <LINK href="Styles.css" type="text/css" rel="stylesheet">
11None.gif    </HEAD>
12None.gif    <body>
13None.gif        <form id="Form1" method="post" runat="server">
14None.gif            <table>
15None.gif                <tr>
16None.gif                    <td>
17None.gif                        <div id="div-datagrid"><asp:datagrid id="grdMytest" runat="server" AutoGenerateColumns="False">
18None.gif                                <Columns>
19None.gif                                    <asp:BoundColumn DataField="ProvName" HeaderText="投标人" ItemStyle-Wrap="False"></asp:BoundColumn>
20None.gif                                </Columns>
21None.gif                            </asp:datagrid></div>
22None.gif                    </td>
23None.gif                </tr>
24None.gif                <tr>
25None.gif                    <td>
26None.gif                        <div id="btn">
27None.gif                            <asp:Button ID="btnOK" Runat="server" Text=" 测 试 "></asp:Button>
28None.gif                        </div>
29None.gif                    </td>
30None.gif                </tr>
31None.gif            </table>
32None.gif        </form>
33None.gif        </TBODY>
34None.gif    </body>
35None.gif</HTML>
ContractedBlock.gif ExpandedBlockStart.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.gif
 12None.gifusing System.Data.SqlClient;
 13None.gifusing  Test1.ItemplateTest;
 14None.gif
 15None.gifnamespace Test1
 16ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 17ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 18InBlock.gif    /// WebForm1 的摘要说明。
 19ExpandedSubBlockEnd.gif    /// </summary>

 20InBlock.gif    public class WebForm1 : System.Web.UI.Page
 21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 22InBlock.gif        protected System.Web.UI.WebControls.Button btnOK;
 23InBlock.gif        protected System.Web.UI.WebControls.DataGrid grdMytest;
 24InBlock.gif
 25InBlock.gif
 26InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 28InBlock.gif            //添加应答列
 29InBlock.gif            TemplateColumn tc1 = new TemplateColumn();
 30InBlock.gif            tc1.ItemTemplate = new CTemplateColLabel("AnserOne");
 31InBlock.gif            tc1.ItemStyle.Wrap = false;
 32InBlock.gif            grdMytest.Columns.Add(tc1);
 33InBlock.gif
 34InBlock.gif            //添加分数列
 35InBlock.gif            TemplateColumn tc2=new TemplateColumn();
 36InBlock.gif            tc2.ItemTemplate=new CTemplateCol("AnserOnePoint");
 37InBlock.gif            tc2.ItemStyle.Wrap = false;
 38InBlock.gif            grdMytest.Columns.Add(tc2);
 39InBlock.gif
 40InBlock.gif            //添加应答列
 41InBlock.gif            TemplateColumn tc3 = new TemplateColumn();
 42InBlock.gif            tc3.ItemTemplate = new CTemplateColLabel("AnserTwo");
 43InBlock.gif            tc3.ItemStyle.Wrap = false;
 44InBlock.gif            grdMytest.Columns.Add(tc3);
 45InBlock.gif
 46InBlock.gif            //添加分数列
 47InBlock.gif            TemplateColumn tc4=new TemplateColumn();
 48InBlock.gif            tc4.ItemTemplate=new CTemplateCol("AnserTwoPoint"); 
 49InBlock.gif            tc4.ItemStyle.Wrap = false;  
 50InBlock.gif            grdMytest.Columns.Add(tc4); 
 51InBlock.gif      
 52InBlock.gif            SqlConnection con = new SqlConnection("Persist Security Info=false;Data Source=192.168.0.9;Initial Catalog=TjgpE;User ID=sa;Password=;");
 53InBlock.gif            SqlDataAdapter da = new SqlDataAdapter("select * from T_NanshouyongTest",con);
 54InBlock.gif            DataTable dt = new DataTable();
 55InBlock.gif            da.Fill(dt);
 56InBlock.gif            grdMytest.DataSource = dt;
 57InBlock.gif            grdMytest.DataBind();
 58ExpandedSubBlockEnd.gif        }

 59InBlock.gif
 60ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
 61InBlock.gif        override protected void OnInit(EventArgs e)
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 63InBlock.gif            //
 64InBlock.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
 65InBlock.gif            //
 66InBlock.gif            InitializeComponent();
 67InBlock.gif            base.OnInit(e);
 68ExpandedSubBlockEnd.gif        }

 69InBlock.gif        
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 71InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 72InBlock.gif        /// 此方法的内容。
 73ExpandedSubBlockEnd.gif        /// </summary>

 74InBlock.gif        private void InitializeComponent()
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
 76InBlock.gif            this.grdMytest.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.grdMytest_ItemCreated);
 77InBlock.gif            this.grdMytest.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.grdMytest_ItemDataBound);
 78InBlock.gif            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
 79InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
 80InBlock.gif
 81ExpandedSubBlockEnd.gif        }

 82ExpandedSubBlockEnd.gif        #endregion

 83InBlock.gif
 84InBlock.gif        private void grdMytest_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 86InBlock.gif            if(e.Item.ItemType == ListItemType.Header)
 87ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 88InBlock.gif                e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMothodItem));//输出流重定向(按项目)
 89ExpandedSubBlockEnd.gif            }

 90ExpandedSubBlockEnd.gif        }

 91InBlock.gif
 92InBlock.gif        private void NewRenderMothodItem(HtmlTextWriter writer,Control ctl)
 93ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 94InBlock.gif            //输出第一行第一列(投标人名称)
 95InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Rowspan,"2");
 96InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Class,"locked");
 97InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Wrap,"false");
 98InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Th);
 99InBlock.gif                Label lbl0 = new Label();
100InBlock.gif                lbl0.Text = "投标人名称";
101InBlock.gif                lbl0.Style.Add("wrap","false");
102InBlock.gif                lbl0.RenderControl(writer);
103InBlock.gif            writer.RenderEndTag();
104InBlock.gif
105InBlock.gif            //输出要合并的第一行
106InBlock.gif
107InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Colspan,"4");
108InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Th);
109InBlock.gif                Label lbl1 = new Label();
110InBlock.gif                lbl1.Text = "资信要求";
111InBlock.gif                lbl1.RenderControl(writer);
112InBlock.gif            writer.RenderEndTag();
113InBlock.gif            //强制结束换行
114InBlock.gif            writer.Write("</tr>");
115InBlock.gif
116InBlock.gif            //添加一行
117InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
118InBlock.gif            //添加合并的第二行第一列
119InBlock.gif
120InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Colspan,"2");
121InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Th);
122InBlock.gif                Label lbl2 = new Label();
123InBlock.gif                lbl2.Text = "问:质保";
124InBlock.gif                lbl2.RenderControl(writer);
125InBlock.gif            writer.RenderEndTag();
126InBlock.gif
127InBlock.gif            //添加合并的第二行第二列   
128InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Colspan,"2");
129InBlock.gif            writer.RenderBeginTag(HtmlTextWriterTag.Th);
130InBlock.gif                Label lbl3 = new Label();
131InBlock.gif                lbl3.Text = "问:售后";
132InBlock.gif                lbl3.RenderControl(writer);
133InBlock.gif            writer.RenderEndTag();
134InBlock.gif
135ExpandedSubBlockEnd.gif        }

136InBlock.gif        private void grdMytest_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
137ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
138InBlock.gif            if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
139ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
140InBlock.gif                e.Item.Cells[0].CssClass = "locked";
141ExpandedSubBlockEnd.gif            }

142ExpandedSubBlockEnd.gif        }

143InBlock.gif
144InBlock.gif        private void btnOK_Click(object sender, System.EventArgs e)
145ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
146InBlock.gif            foreach(DataGridItem item in grdMytest.Items)
147ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
148InBlock.gif                TextBox txtPoint = (TextBox)item.FindControl("AnserOnePoint");
149InBlock.gif                if(txtPoint != null)
150ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
151InBlock.gif                    Response.Write("<script>alert('"+txtPoint.Text.Trim()+"')</script>");
152InBlock.gif                    return;
153ExpandedSubBlockEnd.gif                }

154ExpandedSubBlockEnd.gif            }

155ExpandedSubBlockEnd.gif        }

156ExpandedSubBlockEnd.gif    }

157ExpandedBlockEnd.gif}

两个自定义模板列,如前面的动态生成例子。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值