DataGrid.新增功能“滚动条、固定列头和首列和最后一行、用鼠标调整列宽

新增功能:
1、滚动条
2、固定列头、首列和最后一行
3、用鼠标调整列宽

胡乱写的东西,也没有经过仔细测试,不知道对大家有用没有,还请多提宝贵意见,有什么只管砸来就!
唉哟,这么就有鸡蛋飞来啦……

注意事项:
1、使用新增功能2时,要设置其背景色。否则,它就是透明的,就能看到它下面的内容。
2、用鼠标调整时,不要太靠近边线,否则,不容易调整。
**********************************
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;

namespace SyDG
{
 /// <summary>
 /// WebCustomControl1 的摘要说明。
 /// </summary>
 [DefaultProperty("Text"),
 ToolboxData("<{0}:SyDataGrid runat=server></{0}:SyDataGrid1>")]
 public class SyDataGrid : System.Web.UI.WebControls.DataGrid
 {
  private bool showScroll=true;
  [Category("属性"),Description("是否显示滚动条"),DefaultValue(true)]
  public bool ShowScroll
  {
   get
   {
    return showScroll;
   }
   set
   {
    showScroll=value;
   }
  }
  private string scrollClassName="";
  [Category("属性"),Description("滚动条样式,也可设置外边框样式"),DefaultValue("")]
  public string ScrollClassName
  {
   get
   {
    return scrollClassName;
   }
   set
   {
    scrollClassName=value;
   }
  }
  private bool fixFoot=false;
  [Category("属性"),Description("是否固定显示最后一行在底部"),DefaultValue(false)]
  public bool FixFoot
  {
   get
   {
    return fixFoot;
   }
   set
   {
    fixFoot=value;
   }
  }
  private bool fixFirst=true;
  [Category("属性"),Description("是否固定显示首列在左侧"),DefaultValue(true)]
  public bool FixFirst
  {
   get
   {
    return fixFirst;
   }
   set
   {
    fixFirst=value;
   }
  }
  private bool resizeColumn=true;
  [Category("属性"),Description("是否允许用鼠标调整列宽"),DefaultValue(true)]
  public  bool ResizeColumn
  {
   get
   {
    return resizeColumn;
   }
   set
   {
    resizeColumn=value;
   }
  }
  private Unit tableWidth=new Unit(null);
  [Category("属性"),Description("用来显示数据的表格的实际宽度")]
  public Unit TableWidth
  {
   get{
    return tableWidth;
   }
   set{
    tableWidth=value;
   }
  }
  private string ColorToString(Color color)
  {
   if(color.IsEmpty)
    return "#ffffff";
   else
   {
    if(color.IsKnownColor)
     return color.Name;
    else
     return "#"+color.Name.Substring(2);
   }
  }
/// <summary>
  /// 将此控件呈现给指定的输出参数。
  /// </summary>
  /// <param name="output"> 要写出到的 HTML 编写器 </param>
  protected override void Render(HtmlTextWriter output)
  {
   output.WriteLine("<!--****************^_^**************输出脚本开始****************^_^**************-->");
   //*******************************************************
   if(this.ShowScroll)//如果显示滚动条,输出Div
   {
    //判断在当前页面中是否已经输出过该脚本
    if(this.Page.IsStartupScriptRegistered("SyDG_JScript")==false)
    {
     this.Page.RegisterStartupScript("SyDG_JScript","");//设置输出脚本标记
     string script="";
     script += @"<script language=javascript>
      function SyDG_doscroll(div)
      {
       table=div.getElementsByTagName('TABLE')[0];";
     //如果固定首“列”
     if(this.FixFirst)
      script +=@"
       for(i=table.rows.length-1;i>=0;i--)
       {
        table.rows[i].cells[0].style.position='relative';
        if(i!=table.rows.length-1)
         table.rows[i].cells[0].style.borderBottomStyle='none';
        table.rows[i].cells[0].style.left=div.scrollLeft;
       }
       ";
     //设置首“行”不动
     script += @"
       for(i=0;i<table.rows[0].cells.length;i++)
       {
        if(i!=table.rows[0].cells.length-1 && i!=0)
         table.rows[0].cells[i].style.borderRightStyle='none';
        table.rows[0].cells[i].style.zIndex=9999-i;
        table.rows[0].cells[i].style.position='relative';
        table.rows[0].cells[i].style.top=div.scrollTop;
       }
       ";
     //如果固定尾行,则输出脚本
     if(this.fixFoot) script += "SyDG_setBottomTr(table.rows[table.rows.length-1]);";
     
     script +=" }";
     if(this.ResizeColumn)
      script +=
      @"function SyDG_moveOnTd(td)
      {
       if(event.offsetX>td.offsetWidth-10)
        td.style.cursor='w-resize';
       else
        td.style.cursor='default';
       if(td.mouseDown!=null && td.mouseDown==true)
       {
        if(td.oldWidth+(event.x-td.oldX)>0)
         td.width=td.oldWidth+(event.x-td.oldX);
        td.style.width=td.width;
        td.style.cursor='w-resize';
        
        table=td;
        while(table.tagName!='TABLE') table=table.parentElement;
        table.width=td.tableWidth+(td.offsetWidth-td.oldWidth);table.style.width=table.width;
       }
      }
      function SyDG_downOnTd(td)
      {
       if(event.offsetX>td.offsetWidth-10)
       {
        td.mouseDown=true;
        td.oldX=event.x;
        td.oldWidth=td.offsetWidth;
        table=td;while(table.tagName!='TABLE')table=table.parentElement;
        td.tableWidth=table.offsetWidth;
       }
      }";
     if(this.FixFoot)//如果固定尾行,则输出该脚本函数
      script +=@"
      function SyDG_setBottomTr(tr)//设置尾行不动
      {
       div=tr;table=tr;
       while(table.tagName!='TABLE')table=table.parentElement;
       //while(div.tagName!='DIV')div=div.parentElement;
       div=table.parentElement;
       //tr.style.position='relative';
       //tr.style.top=div.offsetHeight+div.scrollTop-table.offsetHeight-16;
       for(i=0;i<tr.cells.length;i++)
       {
        
        tr.cells[i].style.zIndex=9999-i;
        tr.cells[i].style.position='relative';
        if(i!=tr.cells.length-1 && i!=0)
         tr.cells[i].style.borderRightStyle='none';
        tr.cells[i].style.top=div.offsetHeight+div.scrollTop-table.offsetHeight-16;
       }
      }";
     script += "</script>";
     
     output.Write(script);//输出脚本
    }
    string
div=@"<div id='"+this.ID+"_div' style='OVERFLOW: scroll;width:"+this.Width.Value;
    if(this.Width.Type==UnitType.Percentage)//宽是百分比
     div += "%;";
    else
     div +="px;";
    div += "height:"+this.Height.Value;
    if(this.Height.Type==UnitType.Percentage)
     div +="%;";
    else
     div += "px;";
    div += "' οnscrοll='SyDG_doscroll(this)' οnresize='this.onscroll()' class='"+this.ScrollClassName+"'>";
    output.Write(div);
   }
   output.WriteLine("<!--****************^_^**************输出脚本结束****************^_^**************-->");
   output.WriteLine("<!--****************^_^**************输出控件开始****************^_^**************-->");
   if(this.ShowScroll)
    this.Attributes.Add("onresize","this.parentElement.onscroll()");
   //***************************************************
   Unit width=this.Width;Unit height=this.Height;
   this.Width=this.TableWidth;this.Height=new Unit(null);
   base.Render(output);//调用基类函数输出数据
   this.Width=width;this.Height=height;
   //***************************************************
   output.WriteLine("<!--****************^_^**************输出控件结束****************^_^**************-->");
   if(this.ShowScroll)
   {
    if(this.FixFoot)//如果固定最后一行,输出固定行脚本
    {
     output.Write(@"<script language=javascript>
        table=document.all."
+this.ID+@"_div.getElementsByTagName('TABLE')[0];
        SyDG_setBottomTr(table.rows[table.rows.length-1]);
       </script>");
    }
    output.WriteLine("</div>");//输出DIV结束标记
   }
  }
  protected override void OnItemCreated(DataGridItemEventArgs e)
  {
   if(e.Item.ItemType == ListItemType.Pager)
   {
    for(int i=0;i<e.Item.Cells.Count;i++)
    {
     e.Item.Cells[i].BackColor=e.Item.BackColor;
     //e.Item.Cells[i].Attributes.Add("onclick","alert('Footer')");
    }
   }
   //设置页眉事件,使之可以用鼠标调整列宽
   else if(e.Item.ItemType == ListItemType.Header)
   {
    if(this.ResizeColumn)//如果允许改变列宽
    {
     for(int i=0;i<e.Item.Cells.Count;i++)
     {
      e.Item.Cells[i].Attributes.Add("onmousemove","SyDG_moveOnTd(this)");
      e.Item.Cells[i].Attributes.Add("onmousedown","SyDG_downOnTd(this)");
      e.Item.Cells[i].Attributes.Add("onmouseup","this.mouseDown=false");
      e.Item.Cells[i].Attributes.Add("onmouseout","this.mouseDown=false");
     }
    }
   }
   
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值