(C#版)*控件*有横向和竖向的滚动条的DataGrid(横向支持从第几列滚动)

原创 2004年12月14日 17:28:00

(添加引用Microsoft.VisualBasic)
using System.ComponentModel;
using System.Collections;
using System.Web.UI;
using System.Drawing;
using System.Web;
using Microsoft.VisualBasic;
using System.Web.UI.WebControls;

namespace XYScrollDataGridcsharp{
 [DefaultProperty("Text"), ToolboxData("<{0}:XYScrollDataGridcsharp runat=server></{0}:XYScrollDataGridcsharp>")]
 public class XYScrollDataGridcsharp :System.Web.UI.WebControls.DataGrid
 {
  private Unit mScrollWidth;
  private DataModeType mDataMode;
  private int mFixedColumns;
  private ShowScrollDataMode mShowScrollDataMode;
  private Unit ymScrollWidth;

  public enum ShowScrollDataMode
  {
   XScroll,
   YScroll,
   Both,
   None,
  }
  public enum DataModeType
  {
   可変行Scroll,
   固定行Scroll,
  }


  [NotifyParentProperty(true), Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
  public override bool AutoGenerateColumns
  {
   get
   {
    return base.AutoGenerateColumns;
   }
   set
   {
    base.AutoGenerateColumns = false;
   }
  }
 
  [NotifyParentProperty(true)]
  public override bool AllowCustomPaging
  {
   get
   {
    return base.AllowCustomPaging;
   }
   set
   {
    base.AllowCustomPaging = value;
   }
  }

  [NotifyParentProperty(true)]
  public override bool AllowPaging
  {
   get
   {
    return base.AllowPaging;
   }
   set
   {
    base.AllowPaging = value;
   }
  }


  [NotifyParentProperty(true), Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
  public override bool AllowSorting
  {
   get
   {
    return base.AllowSorting;
   }
   set
   {
    base.AllowSorting = value;
   }
  }

  [NotifyParentProperty(true)]
  public override TableItemStyle AlternatingItemStyle
  {
   get
   {
    return base.AlternatingItemStyle;
   }
  }

  [NotifyParentProperty(true)]
  public override Color BackColor
  {
   get
   {
    return base.BackColor;
   }
   set
   {
    base.BackColor = value;
   }
  }

  [NotifyParentProperty(true)]
  public override string BackImageUrl
  {
   get
   {
    return base.BackImageUrl;
   }
   set
   {
    base.BackImageUrl = value;
   }
  }

  [NotifyParentProperty(true)]
  public override Color BorderColor
  {
   get
   {
    return base.BorderColor;
   }
   set
   {
    base.BorderColor = value;
   }
  }

  [NotifyParentProperty(true)]
  public override BorderStyle BorderStyle
  {
   get
   {
    return base.BorderStyle;
   }
   set
   {
    base.BorderStyle = value;
   }
  }

  [NotifyParentProperty(true)]
  public override Unit BorderWidth
  {
   get
   {
    return base.BorderWidth;
   }
   set
   {
    base.BorderWidth = value;
   }
  }

  [NotifyParentProperty(true)]
  public override int CellPadding
  {
   get
   {
    return base.CellPadding;
   }
   set
   {
    base.CellPadding = value;
   }
  }

  [NotifyParentProperty(true)]
  public override int CellSpacing
  {
   get
   {
    return base.CellSpacing;
   }
   set
   {
    base.CellSpacing = value;
   }
  }

  [NotifyParentProperty(true)]
  public override DataGridColumnCollection Columns
  {
   get
   {
    return base.Columns;
   }
  }


  [NotifyParentProperty(true)]
  public override int EditItemIndex
  {
   get
   {
    return base.EditItemIndex;
   }
   set
   {
    base.EditItemIndex = value;
   }
  }

  [NotifyParentProperty(true)]
  public override TableItemStyle EditItemStyle
  {
   get
   {
    return base.EditItemStyle;
   }
  }

  [NotifyParentProperty(true)]
  public override bool Enabled
  {
   get
   {
    return base.Enabled;
   }
   set
   {
    base.Enabled = value;
   }
  }

  [NotifyParentProperty(true)]
  public override TableItemStyle FooterStyle
  {
   get
   {
    return base.FooterStyle;
   }
  }

  [NotifyParentProperty(true)]
  public override Color ForeColor
  {
   get
   {
    return base.ForeColor;
   }
   set
   {
    base.ForeColor = value;
   }
  }

  [NotifyParentProperty(true)]
  public override GridLines GridLines
  {
   get
   {
    return base.GridLines;
   }
   set
   {
    base.GridLines = value;
   }
  }

  [NotifyParentProperty(true)]
  public override TableItemStyle HeaderStyle
  {
   get
   {
    return base.HeaderStyle;
   }
  }

  [NotifyParentProperty(true)]
  public override Unit Height
  {
   get
   {
    return base.Height;
   }
   set
   {
    base.Height = value;
   }
  }

  [NotifyParentProperty(true)]
  public override HorizontalAlign HorizontalAlign
  {
   get
   {
    return base.HorizontalAlign;
   }
   set
   {
    base.HorizontalAlign = value;
   }
  }

  [NotifyParentProperty(true)]
  public override TableItemStyle ItemStyle
  {
   get
   {
    return base.ItemStyle;
   }
  }

  [NotifyParentProperty(true)]
  public override DataGridPagerStyle PagerStyle
  {
   get
   {
    return base.PagerStyle;
   }
  }

  [NotifyParentProperty(true)]
  public override int PageSize
  {
   get
   {
    return base.PageSize;
   }
   set
   {
    base.PageSize = value;
   }
  }

  [NotifyParentProperty(true)]
  public override bool ShowFooter
  {
   get
   {
    return base.ShowFooter;
   }
   set
   {
    base.ShowFooter = value;
   }
  }

  [NotifyParentProperty(true)]
  public override bool ShowHeader
  {
   get
   {
    return base.ShowHeader;
   }
   set
   {
    base.ShowHeader = value;
   }
  }


  [NotifyParentProperty(true)]
  public override bool Visible
  {
   get
   {
    return base.Visible;
   }
   set
   {
    base.Visible = value;
   }
  }

  [NotifyParentProperty(true)]
  public override Unit Width
  {
   get
   {
    return base.Width;
   }
   set
   {
    base.Width = value;
   }
  }

  [NotifyParentProperty(true)]
  public override string CssClass
  {
   get
   {
    return base.CssClass;
   }
   set
   {
    base.CssClass = value;
   }
  }

  [Category("モード選択"), DefaultValue(typeof(Unit), "0px"), Description("スクロールの幅")]
  public Unit YScrollBarHeight
  {
   get
   {
    return ymScrollWidth;
   }
   set
   {
    ymScrollWidth = value;
   }
  }

  [Category("モード選択"), DefaultValue(typeof(Unit), "0px"), Description("スクロールの幅")]
  public Unit XScrollBarWidth
  {
   get
   {
    return mScrollWidth;
   }
   set
   {
    mScrollWidth = value;
   }
  }

  [Category("/u30e2/u30fc/u30c9/u9078/u629e"), Description("/u56fa/u5b9a/u884c/u306e/u500b/u6570"), DefaultValue(0)]
  public int FixedColumns
  {
   get
   {
   
    if (this.mShowScrollDataMode == ShowScrollDataMode.XScroll)
    {
     return this.mFixedColumns;
    }
    return 0;
   }
   set
   {
    if (this.mShowScrollDataMode != ShowScrollDataMode.XScroll)
    {
     return;
    }
    if ((this.Columns.Count < value) & (this.Columns.Count != 0))
    {
     this.mFixedColumns = this.Columns.Count;
    }
    else if (value < 0)
    {
     this.mFixedColumns = 0;
    }
    else
    {
     this.mFixedColumns = value;
    }
   }
  }


  [Category("モード選択"), DefaultValue(typeof(ShowScrollDataMode), "XScroll"), Description("XScroll")]
  public ShowScrollDataMode ShowScrollMode
  {
   get
   {
    return mShowScrollDataMode;
   }
   set
   {
    mShowScrollDataMode = value;
   }
  }

  protected override void Render(HtmlTextWriter writer)
  {
   Hashtable hashtable1 = new Hashtable();
   hashtable1.Add("Z-INDEX", this.Style["Z-INDEX"]);
   hashtable1.Add("LEFT", this.Style["LEFT"]);
   hashtable1.Add("POSITION", this.Style["POSITION"]);
   hashtable1.Add("TOP", this.Style["TOP"]);
   this.Style.Remove("Z-INDEX");
   this.Style.Remove("LEFT");
   this.Style.Remove("POSITION");
   this.Style.Remove("TOP");
   writer.Write("<div id='" + this.ID + "_All' style='");
   writer.Write("Z-INDEX: ", hashtable1["Z-INDEX"]);
   writer.Write("LEFT: ", hashtable1["LEFT"]);
   writer.Write("POSITION: ", hashtable1["POSITION"]);
   writer.Write("TOP: ", hashtable1["TOP"], "' ms_positioning = 'FlowLayout'>");
   this.FixedColumnDataGrid(writer);
   writer.Write("</div>");

  }

  private void FixedColumnDataGrid(HtmlTextWriter output)
  {
   string text2;
   string text4;
   text2="";
   text4="";
   bool[] flagArray1 = new bool[(this.Columns.Count - 1) + 1];

   int num7 = this.Columns.Count - 1;

   for (int num2 = 0; num2 <= num7; num2++)
   {
    flagArray1[num2] = this.Columns[num2].Visible;
   }
   int num1 = 0;
   int num6 = this.mFixedColumns - 1;
   for (int num3 = 0; num3 <= num6; num3++)
   {
    int num5 = this.Columns.Count - 1;
    for (int num4 = num1; num4 <= num5; num4++)
    {
     if (this.Columns[num4].Visible)
     {
      text4 = System.Convert.ToString(Conversion.Val(text4) + Conversion.Val(this.Columns[num4].HeaderStyle.Width.ToString()));
      num1 = num4 + 1;
      break;
     }
    }
   }
  
   text4 = System.Convert.ToString((Conversion.Val(text4) + 1) + (Conversion.Val(this.CellSpacing) * this.mFixedColumns));
   if (this.mFixedColumns == 0)
   {
    text4 = "0";
   }
   string text5 = this.mScrollWidth.ToString();
   string text3 = this.mScrollWidth.ToString();
   this.OutPutLayout(text4, text5, text2, text3, flagArray1, output);
  }


  private void OutPutLayout(string widthOne, string widthTwo, string heightOne, string heightTwo, bool[] visiableColumn, HtmlTextWriter output)
  {
   output.Write("<table cellSpacing='0' cellPadding='0' border='0'>");
   if (this.ShowScrollMode == ShowScrollDataMode.None)
   {
    output.Write("<tr>");
    if (this.Columns.Count > this.mFixedColumns)
    {
     this.ShowLeft("", output);
    }
    output.Write("</tr>");
   }
   else if (this.ShowScrollMode ==ShowScrollDataMode.XScroll)
   {
    output.Write("<tr>");
    this.ShowLeft(widthOne, output);
    if (this.Columns.Count > this.mFixedColumns)
    {
     this.ShowRight(widthTwo, output);
    }
    output.Write("</tr>");
   }
   else if (this.ShowScrollMode == ShowScrollDataMode.YScroll)
   {
    output.Write("<tr>");
    this.ShowTop(heightOne, output);
    output.Write("</tr>");
    if (this.Columns.Count > this.mFixedColumns)
    {
     output.Write("<tr>");
     this.ShowBottom(heightTwo, output);
     output.Write("</tr>");
    }
   }
   else if (this.ShowScrollMode == ShowScrollDataMode.Both)
   {
    output.Write("<tr>");
    this.ShowTop(heightOne, output);
    output.Write("</tr>");
    if (this.mFixedColumns == 0)
    {
     output.Write("<tr>");
     this.BothScroll(heightTwo, output);
     output.Write("</tr>");
    }
   }
   int num2 = this.Columns.Count - 1;
   for (int num1 = 0; num1 <= num2; num1++)
   {
    this.Columns[num1].Visible = visiableColumn[num1];
   }
   output.Write("</table>");
  }

  private void BothScroll(string width, HtmlTextWriter output)
  {
   output.Write("<td valign='top'>");
   string[] textArray1 = new string[6] { "<DIV id='", this.ID, "_DivBottom' style='OVERFLOW-Y: scroll; OVERFLOW-X: scroll;HEIGHT: ", this.YScrollBarHeight.ToString(), "; WIDTH: ", this.XScrollBarWidth.ToString() } ;
   string text1 = string.Concat(textArray1) + "; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; ' ms_positioning = 'GridLayout' > ";
   output.Write(text1);
   int num4 = this.Items.Count - 1;
   for (int num1 = 0; num1 <= num4; num1++)
   {
    this.Items[num1].Visible = true;
   }
   int num3 = this.Items.Count - 1;
   for (int num2 = 0; num2 <= num3; num2++)
   {
    this.Items[num2].Attributes["id"] = this.ID + "TRD" + num2;
   }
   this.Style["BORDER-STYLE"] = "none";
   this.ShowHeader = false;
   base.Render(output);
   this.ShowHeader = true;
   output.Write("</DIV>");
   output.Write("</td>");
  }

  private void ShowBottom(string height, HtmlTextWriter output)
  {
   output.Write("<td valign='top'>");
   string text1 = ("<DIV id='" + this.ID + "_DivBottom' style='OVERFLOW-Y: scroll; HEIGHT: " + this.YScrollBarHeight.ToString()) + "; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; ' ms_positioning = 'GridLayout' > ";
   output.Write(text1);
   int num4 = this.Items.Count - 1;
   for (int num1 = 0; num1 <= num4; num1++)
   {
    this.Items[num1].Visible = true;
   }
   int num3 = this.Items.Count - 1;
   for (int num2 = 0; num2 <= num3; num2++)
   {
    this.Items[num2].Attributes["id"] = this.ID + "TRD" + num2;
   }
   this.Style["BORDER-STYLE"] = "none";
   this.ShowHeader = false;
   base.Render(output);
   this.ShowHeader = true;
   output.Write("</DIV>");
   output.Write("</td>");
  }

  private void ShowLeft(string width, HtmlTextWriter output)
  {
   output.Write("<td valign='top'>");
   string text1 = ("<DIV id='" + this.ID + "_DivLeft' style='OVERFLOW-X: hidden; WIDTH: " + width) + "; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; ' ms_positioning = 'GridLayout' > ";
   output.Write(text1);
   if (this.mShowScrollDataMode == ShowScrollDataMode.None)
   {
    int num4 = this.Items.Count - 1;
    for (int num1 = 0; num1 <= num4; num1++)
    {
     this.Items[num1].Attributes["id"] = this.ID + "TRD" + num1;
    }
   }
   else
   {
    int num3 = this.Items.Count - 1;
    for (int num2 = 0; num2 <= num3; num2++)
    {
     this.Items[num2].Attributes["id"] = this.ID + "TRL" + num2;
    }
   }
   this.Style["BORDER-STYLE"] = "none";
   base.Render(output);
   output.Write("</DIV>");
   output.Write("</td>");
  }

  private void ShowRight(string width, HtmlTextWriter output)
  {
   output.Write("<td valign='top'>");
   string text1 = ("<DIV id='" + this.ID + "_DivRight' style='OVERFLOW-X: scroll; WIDTH: " + width) + "; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; ' ms_positioning = 'GridLayout' > ";
   output.Write(text1);
   if (this.ShowScrollMode != ShowScrollDataMode.None)
   {
    int num6 = this.mFixedColumns - 1;
    for (int num1 = 0; num1 <= num6; num1++)
    {
     int num5 = this.Columns.Count - 1;
     for (int num2 = 0; num2 <= num5; num2++)
     {
      if (this.Columns[num2].Visible)
      {
       this.Columns[num2].Visible = false;
       break;
      }
     }
    }
   }
   int num4 = this.Items.Count - 1;
   for (int num3 = 0; num3 <= num4; num3++)
   {
    this.Items[num3].Attributes["id"] = this.ID + "TRR" + num3;
   }
   this.Style["BORDER-STYLE"] = "none";
   base.Render(output);
   output.Write("</DIV>");
   output.Write("</td>");
  }

  private void ShowTop(string height, HtmlTextWriter output)
  {
   output.Write("<td valign='bottom' align='left'>");
   string text1 = ("<DIV id='" + this.ID) + "_DivTop' style='BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; ' ms_positioning = 'GridLayout' > ";
   output.Write(text1);
   int num4 = this.Items.Count - 1;
   for (int num1 = 0; num1 <= num4; num1++)
   {
    this.Items[num1].Visible = false;
   }
   int num3 = this.Items.Count - 1;
   for (int num2 = 0; num2 <= num3; num2++)
   {
    this.Items[num2].Attributes["id"] = this.ID + "TRD" + num2;
   }
   this.Style["BORDER-STYLE"] = "none";
   this.ShowFooter = false;
   base.Render(output);
   output.Write("</DIV>");
   output.Write("</td>");
  }
 }
}

datagrid中添加滚动条

DATAGRID中是没滚动条的,要添加的话其实很简单,只需要: 其实是利用了CSS属性...
  • xiaoxiaohai123
  • xiaoxiaohai123
  • 2007年05月18日 10:22
  • 1829

datagridviwe的简单总结

貌似做winform程序很多都用到了datagridview这个控件啊,我也用了不少了,总结一下吧。这个控件貌似可以直接绑定数据源,不过我基本没用过绑定的,都是写代码,将查询到的表fill到ds里面,...
  • cyn126
  • cyn126
  • 2011年07月08日 09:53
  • 492

老程序员学C# ------操作DataGridView控件详解

    在项目中,老程序员都知道显示数据的控件的操作灵活性很重要。Visual Studio 2008里面提供了一个很强大的数据显示控件------------DataGridView。DataGri...
  • lanhai96
  • lanhai96
  • 2010年05月25日 00:09
  • 115591

*控件*有横向和竖向的滚动条的DataGrid(横向支持从第几列滚动)

(不好意思,刚才有人要横竖都有,我就改了一下,可以都出现横向和竖向滚动,q其实我觉得没必要了,要不然,微软干嘛要用PAGESIZE来控制呀)目前在论坛有很多的人问有滚动条的DATAGRID,所以我就急...
  • rickjelly2004
  • rickjelly2004
  • 2004年12月14日 09:11
  • 3765

【easyui】datagrid横向滚动条自动滚动功能

在datagrid的onLoadSuccess函数中,做如下处理//根据当前时刻,判断滚动条滑动的位置 var myDate = new Date(); ...
  • xuexiiphone
  • xuexiiphone
  • 2017年05月09日 14:22
  • 1150

easyui-tabs(选项卡)重新打开,数据没有变化!

问题1: 今天在做宿舍管理系统优化的时候,发现一个问题。在弹窗的基础上在弹一个Tabs页面,关闭之后再打开一个新的Tabs页面,结果显示的还是上一个页面的数据。没有随着页面的更改,而更改。这样也属于一...
  • younghaiqing
  • younghaiqing
  • 2017年03月10日 14:26
  • 695

silverlight之How To:格式化DataGrid里的列显示

 DataGrid里的列可以很方便的绑定到数据源里的某个属性,但是有时候需要格式化这个属性值的显示,比较典型的就是属性是DateTime类型的,当只想显示以特定格式显示日期部分,这个时候怎么做呢?首先...
  • weiky626
  • weiky626
  • 2008年12月21日 18:42
  • 4088

WPF DataGrid默认显示垂直和横向滚动条-技术&分享

WPF DataGrid默认显示垂直滚动条
  • jackson0714
  • jackson0714
  • 2014年05月06日 18:47
  • 7242

datagrid的列非常多的时候,而且没有表格数据,横向滚动条不会显示

datagrid的列非常多的时候,而且没有表格数据,横向滚动条不会显示,字段无法读取...
  • zhu_nana
  • zhu_nana
  • 2015年12月08日 11:06
  • 1027

[Winform]C#常用控件缩写

控件名称 缩写 Buttom按钮 Btn CheckBox复选框 Chk ColumnHeader视图列表头 ...
  • david_520042
  • david_520042
  • 2012年09月21日 09:04
  • 1963
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:(C#版)*控件*有横向和竖向的滚动条的DataGrid(横向支持从第几列滚动)
举报原因:
原因补充:

(最多只允许输入30个字)