简单实用的DATAGRID组件

近期由于项目中多处需要用到可固定表头,表体可滚动的DATAGRID。
在网上查阅了一些资料大都是和没封装好的DATAGRID,在实际项目中应用比较繁琐。
在参考:
http://www.datawebcontrols.com/faqs/CustomizingAppearance/ScrollableDataGridWithFixedHeader.shtml

的基础上封装了一个无分页的可固定表头,表体可移动的DATAGRID组件,希望可以为需要此简单功能的同仁提供一点帮助。

None.gif <style type="text/css">
ExpandedBlockStart.gifContractedBlock.gif.DataGridFixedHeader 
{dot.gif} { FONT-WEIGHT: bold; COLOR: white; POSITION: relative; ; TOP: expression(this.offsetParent.scrollTop - 2); BACKGROUND-COLOR: gray }
None.gif</style>


 

  1 None.gif using  System;
  2 None.gif using  System.Web.UI;
  3 None.gif using  System.Web.UI.WebControls;
  4 None.gif using  System.ComponentModel;
  5 None.gif using  System.Diagnostics;
  6 None.gif using  System.IO;
  7 None.gif using  System.Text;
  8 None.gif using  System.Drawing;
  9 None.gif
 10 None.gif
 11 None.gif namespace  CustomControls
 12 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 14InBlock.gif    /// CustomDataGrid 的摘要说明。
 15InBlock.gif    /// Function:Fixed Header and Scrollable DataGrid control 
 16ExpandedSubBlockEnd.gif    /// </summary>

 17InBlock.gif    public class CustomDataGrid:System.Web.UI.WebControls.DataGrid
 18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 19InBlock.gif        public CustomDataGrid():base()
 20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 21InBlock.gif            //
 22InBlock.gif            // TODO: 在此处添加构造函数逻辑
 23InBlock.gif            //
 24InBlock.gif            this.HeaderStyle.BackColor=System.Drawing.ColorTranslator.FromHtml("#D8C8A8");
 25InBlock.gif            this.Style.Add("font-size","10pt");
 26InBlock.gif            this.BorderWidth=0;
 27InBlock.gif            this.CellSpacing=1;
 28InBlock.gif            this.CellPadding=3;
 29InBlock.gif            this.AlternatingItemStyle.BackColor=System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
 30InBlock.gif            this.ItemStyle.BackColor=System.Drawing.ColorTranslator.FromHtml("#DDDDDD");
 31InBlock.gif
 32InBlock.gif            this.AllowPaging=true;
 33InBlock.gif            this.AllowSorting=true;
 34InBlock.gif            this.AllowCustomPaging=false;
 35InBlock.gif            this.AutoGenerateColumns=false;
 36InBlock.gif            this.PagerStyle.Visible=false;
 37InBlock.gif            HeaderStyle.Wrap = false;
 38InBlock.gif            ItemStyle.Wrap = false;
 39InBlock.gif            this.FooterStyle.Wrap = false;
 40InBlock.gif            this.HeaderStyle.Height=18;
 41InBlock.gif            ItemStyle.Height = 15;
 42InBlock.gif            //header style
 43InBlock.gif            this.HeaderStyle.CssClass = "ms-formlabel DataGridFixedHeader";
 44ExpandedSubBlockEnd.gif        }

 45InBlock.gif        protected override void Render(HtmlTextWriter output)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            //Actual length
 48InBlock.gif            string actwidth = (this.Width.Type==UnitType.Pixel)?(Width.Value+20)+"px":(Width.Value-5)+"%";
 49InBlock.gif            output.WriteBeginTag("div");
 50InBlock.gif            output.WriteAttribute("id",ID + "_div");
 51InBlock.gif            output.WriteAttribute("style",
 52InBlock.gif                "HEIGHT: " + Height + ";" +
 53InBlock.gif                "WIDTH: " + actwidth+";"+
 54InBlock.gif                "OVERFLOW: auto;"+
 55InBlock.gif                "TABLE-LAYOUT:fixed;"
 56InBlock.gif                );
 57InBlock.gif            output.Write(HtmlTextWriter.TagRightChar);
 58InBlock.gif            base.Render(output);
 59InBlock.gif            output.WriteEndTag("div");
 60InBlock.gif            //add script for adjust Height
 61InBlock.gif            string adjustHeightScript = @"<script>try{"+this.ID+".style.height=\"\"}catch(e){}</script>";
 62InBlock.gif            Page.RegisterStartupScript("dummyKey" + this.ID, adjustHeightScript);
 63ExpandedSubBlockEnd.gif        }
         
 64InBlock.gif
 65InBlock.gif        protected override void OnInit(EventArgs e)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 67InBlock.gif            //default height and wedth
 68InBlock.gif            if (0 == Width.Value) Width = new Unit("300px");
 69InBlock.gif            if (0 == Height.Value) Height = new Unit("150px");
 70InBlock.gif            base.OnInit (e);
 71ExpandedSubBlockEnd.gif        }

 72InBlock.gif
 73InBlock.gif        protected override void OnItemCreated(DataGridItemEventArgs e)
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75InBlock.gif            for(int i=0;i<e.Item.Cells.Count;i++)
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 77InBlock.gif                //
 78InBlock.gif                e.Item.Cells[i].Attributes.Add("nowrap","nowrap");
 79ExpandedSubBlockEnd.gif            }

 80InBlock.gif            if(e.Item.ItemType == ListItemType.AlternatingItem ||e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.SelectedItem)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 82InBlock.gif
 83InBlock.gif                e.Item.Attributes["onmouseover"= "javascript:this.style.backgroundColor='#fff7ce';";
 84InBlock.gif                if(e.Item.ItemType==ListItemType.Item)
 85InBlock.gif                    e.Item.Attributes["onmouseout"= "javascript:this.style.backgroundColor='#dedfde';";
 86InBlock.gif                else
 87InBlock.gif                    e.Item.Attributes["onmouseout"= "javascript:this.style.backgroundColor='#ffffff';";
 88InBlock.gif
 89ExpandedSubBlockEnd.gif            }

 90ExpandedSubBlockEnd.gif        }

 91InBlock.gif
 92ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 93InBlock.gif        /// The deletion confirms prompts
 94InBlock.gif        /// </summary>
 95ExpandedSubBlockEnd.gif        /// <param name="e"></param>

 96InBlock.gif        protected override void OnItemDataBound(DataGridItemEventArgs e)
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 98InBlock.gif            foreach(TableCell tc in e.Item.Cells)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
100InBlock.gif                if(tc.HasControls())
101ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
102InBlock.gif                    if(tc.Controls[0].GetType().Name=="DataGridLinkButton")
103ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
104InBlock.gif                        LinkButton lb=(LinkButton)tc.Controls[0];
105InBlock.gif                        if(lb.CommandName=="Delete")
106InBlock.gif                            lb.Attributes["onclick"]="javascript:if(!confirm('are you sue?'))return false;";
107ExpandedSubBlockEnd.gif                    }

108ExpandedSubBlockEnd.gif                }

109ExpandedSubBlockEnd.gif            }

110InBlock.gif            
111InBlock.gif            base.OnItemDataBound(e);
112ExpandedSubBlockEnd.gif        }

113ExpandedSubBlockEnd.gif    }

114InBlock.gif
115ExpandedBlockEnd.gif}

116 None.gif
117 None.gif
118 None.gif

功能比较简单,基本功能在项目中已测试。

希望有发现BUG的给予告知,谢谢。


转载于:https://www.cnblogs.com/devuser/archive/2005/10/26/262663.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值