打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Winform DataGridView扩展系列(一)

写一个win form的程序,要用到DataGridView的汇总行,网上找了找不是要钱就是有点小问题。想了想,自己写一个吧,看看以前打下的底子还中不,呵呵。

要写就的有各构想,前前后后想了几天把我要的列了一下:汇总行、多列表头(中国报表)、百分比列、计算器列、时间列、日期列。几个DataGridVewColumn的继承扩展没啥难度,就前两个要费点力。开工,干了。

先看看结果:

a)汇总行(继承DataGridView控件实现)

b)多列表头(组件方式的实现)

 

一、汇总行(继承DataGridView控件)实现:

  1. 要求:

    a.不影响DataGridView本身的运行,并尽量保持与DataGridView的整体外观一致

    b.减少闪烁。

    c.提供较完整的设计时支持。

       2.实现:

    a.类层次结构

      /// <summary>   

      /// 有底部分析区域的DataGridView控件类,继承自DataGridView

      /// </summary>   

      public class FootDataGridView : DataGridView{}

       /// <summary>

      /// Panel控件扩展类,应用于FootDataGridView   

      /// </summary>   

      public class FootBand : Panel

      {

        #region 附属类

        [DefaultProperty("ColumnName")]       

        public class FootItem{}

        [Editor(typeof(ZYQ.Design.Utilities.Editor.DataGridViewFootCollectionEditor),typeof(UITypeEditor))]       

        public class FootItemCollection : List<FootItem>{}

        #endregion

      }

    b.设计时支持

              c.细节与实现:

      属性和方法等的实现没啥好说的,MSDN中都有。难度在呈现UI上。

      为减少闪烁,必须声明开启双缓存。

       private void Initialize()       

      {           

        #region 声明使用双缓存           

        SetStyle(ControlStyles.DoubleBuffer |

                                     ControlStyles.OptimizedDoubleBuffer |

                                     ControlStyles.AllPaintingInWmPaint,

                                     true);

                UpdateStyles();

                #endregion

        ......

      }

      为配合DataGridView的动作使汇总行跟随改变,必须重写一些事件引发的函数来重新计算汇总行绘制所需要的数据,他们是:

      protected override void OnPaint(PaintEventArgs e),

      protected override void OnCellValueChanged(DataGridViewCellEventArgs e)

      protected override void OnColumnWidthChanged(DataGridViewColumnEventArgs e)

      protected override void OnColumnDividerWidthChanged(DataGridViewColumnEventArgs e)

      protected override void OnGridColorChanged(EventArgs e)

      protected override void OnResize(EventArgs e)

      protected override void OnRowHeadersWidthChanged(EventArgs e)

      protected override void OnSizeChanged(EventArgs e)

      protected override void OnVisibleChanged(EventArgs e)

      protected override void OnScroll(ScrollEventArgs e)

      protected override void OnColumnRemoved(DataGridViewColumnEventArgs e)

      protected override void OnBackgroundColorChanged(EventArgs e)

      protected override void OnRowHeadersDefaultCellStyleChanged(EventArgs e)

      protected override void OnColumnStateChanged(DataGridViewColumnStateChangedEventArgs e)

      汇总行的绘制必须是在DataGridView创建后才开始的,所以必须获得DataGridView的HandleCreated事件的通知;而要绘制汇总行必须使用Panel的Paint事件.

       private void Initialize()       

      {

        ......

        base.HandleCreated += new EventHandler(FootDataGridView_HandleCreated);

        _foot.Paint += new PaintEventHandler(_foot_Paint);

        ......

      }

      void FootDataGridView_HandleCreated(object sender, EventArgs e)

            {           

        _foot.ParentIsCreated = this.Created;

           }

      void _foot_Paint(object sender, PaintEventArgs e)

             {                          

       //画分割线

             if (ShowDividedLine)

             {

           OnPaintDividedLine(e.Graphics);

              }

              //画foot header

              if (RowHeadersVisible)

                {

                OnPaintFootHeader(e.Graphics);

                }

                  //画底部区域

                  OnPaintColumnsFoot(e);

      }

      所有重写的事件引发函数调用RaisePanel()函数来引发Panel的Paint事件

      private void RaisePanel()

            {

        if (_foot.Visible)

                {

                     if (Created)

                     {

                           ResetButtomPanel();

                           PaintEventArgs parg = new PaintEventArgs(_foot.CreateGraphics(), _foot.ClientRectangle);

                           ClearFoot(parg.Graphics);

                           _foot_Paint(_foot, parg);

                     }

                }

           }

     绘制过程用GDI+,没什么特别的,只要搞清楚剪辑使用的就行了。

使用下来感觉很好,和DataGridView配合很好,没有闪烁的情况。后续开发的时候可以利用Panel的特性加入鼠标、绑定、重算等功能。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
c# 只能输入数字的文本框
真有这个功能,Winform DataGridView仿Excel下拉复制
怎样用自定义控件编辑DataGridView(WinForm)单元格的值
asp.net 的页面执行顺序 页面事件执行详解
wpf只运行一个实例
ASP.NET 页面执行顺序详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服